How should I use @types with TypeScript 2

It’s very simple. Just install the definitions that you need via npm.

For example if you need lodash you can do:

npm install --save @types/lodash

Once it’s installed you can use it right away in your project. Typescript will resolve the typings for the installed @types package from the node_modules/@types folder by default. There’s no need for a tsd.json or typings.json file anymore.

Additional points:

  • The major and minor version of the @types package in npm should correspond with the package version.
  • You can search for types here: http://microsoft.github.io/TypeSearch/
  • Read about typeRoots and types here. Specifically pay attention to these two points:
    • If typeRoots is specified in tsconfig.json, then only specified folders will be used for the type roots. That will exclude ./npm_modules/@types/ unless you specify it.
    • If types is specified in tsconfig.json, then only the packages specified will be included.

Read more in the blog post here.

Leave a Comment