How to use @types with TypeScript 2

So far we are used to tsd or (best version) typing

But now that TypeScript 2 offers a new @types function, how do I convert my current project to working with @types?

I have tsd.json (typings.json are some cases) with all the dependencies, what are the steps to transition to TypeScript 2? What are the new best practices? Does @types support specific versions?

Thank!

+41
typescript typescript-typings tsd
Jul 18 '16 at 19:10
source share
4 answers

It is very simple. Just install the definitions you need via npm.

For example, if you need lodash, you can do:

 npm install --save @types/lodash 

After installation, you can immediately use it in your project. Typescript will allow typing of the installed @types package from the node_modules / @ folder by default. No need for tsd.json or typings.json file.

Additional items:

  • The major and minor version of @types package in npm should match the package version.
  • Here you can search for types: http://microsoft.imtqy.com/TypeSearch/
  • Read about typeRoots and types here . In particular, pay attention to these two points:
    • If typeRoots is specified in typeRoots , only the specified folders will be used for type roots. This excludes. / npm _modules / @ types / if you do not specify it.
    • If types is specified in tsconfig.json, then only the specified packages will be included.

More details in the blog post here .

+42
Jul 18 '16 at 19:26
source share

Typescript 2.0 gets rid of the previous typing system.
Now Typescript 2.0 should by default look at ./node_modules/@types and get the types that you set as separate node modules, for example. npm install --save @types/react (as mentioned by @David Sherret)

In the current version of Typescript 2.0 beta strong> there is an error that does not load new types. Manually using cmd new tsc compiles the files, but in VS 2015 there is no IntelliSense support, and no errors were found while the .ts file is in edit mode.

To solve this problem, change tsconfig.json to similar settings:

 { "compilerOptions": { // ... other config rows "typeRoots": [ "node_modules/@types/" ], "types": [ "jquery", "react", "react-dom", /*... your other types */ ], } } 

For me, a manual declaration of "types" helped solve this problem, for other guys, "typeRoots" helped. Hope this saves development time.

+38
Aug 24 '16 at 20:26
source share

It looks like they are just npm packages, you can find all supported here .

tsc will display all types in the node_modules folder.

You can move the dependencies that you have in typings.json into package.json (provided that you change the names too).

You can learn more about this here .

+2
Jul 18 '16 at 19:25
source share

how can I convert my current project to working with @types

I definitely recommend staying a little longer.

eg. the problems are still fixed ... just 4 hours ago: https://github.com/Microsoft/TypeScript/issues/9725#issuecomment-233469422

+2
Jul 19 '16 at 1:55
source share



All Articles