Headshot of Marius Schulz
Marius Schulz Front End Engineer

TypeScript 2.0: Acquiring Type Declaration Files

In TypeScript 2.0, it has become significantly easier to acquire type information for JavaScript libraries. There's no longer a need for additional tools such as typings or tsd. Instead, type declaration packages are directly available on npm.

Egghead lesson: Install TypeScript declarations from

Installing Type Declaration Packages from npm

Let's assume you want to use Lodash in one of your TypeScript projects:

npm install --save lodash

After running this command, Lodash is installed into the node_modules folder and is also listed as a dependency within your project's package.json file. However, there's no type information available for TypeScript because Lodash is written in JavaScript and doesn't ship with type declaration files.

Using npm again, you can now install the @types/lodash package which contains the corresponding type declarations that TypeScript needs:

npm install --save @types/lodash

Note how the type declaration package is named after the original npm package, prefixed with @types/. Most packages should follow this convention, but you can always fall back to TypeSearch to find the name of the type declaration package for a given library.

The benefit of using npm for managing type information is that type declaration packages will be listed in your package.json alongside your other dependencies. In addition, they can be properly versioned, just like any other npm package. Plus, you only need a single package manager, npm, that is set up already anyway.

Where Do the Type Declaration Files Go?

Like any other npm package, the type declaration packages are installed in the node_modules folder. Within it, there's a @types folder which contains all typings. In the above example, it'll contain a lodash folder, within which you'll find the index.d.ts file with all the Lodash type information:

@types folder

The TypeScript compiler understands this convention and will pick up type declarations within the @types folder automatically. There's no need to update the tsconfig.json or any other configuration file.

Who Creates the Type Declaration Packages?

Behind the scenes, the type declaration packages are automatically created by the types-publisher service. It publishes the contents of the DefinitelyTyped repository to npm. That way, typings contributed to DefinitelyTyped end up on npm, ready for consumption.

This post is part of the TypeScript Evolution series:

  1. TypeScript 2.0: Non-Nullable Types
  2. TypeScript 2.0: Control Flow Based Type Analysis
  3. TypeScript 2.0: Acquiring Type Declaration Files
  4. TypeScript 2.0: Read-Only Properties
  5. TypeScript 2.0: Tagged Union Types
  6. TypeScript 2.0: More Literal Types
  7. TypeScript 2.0: The never Type
  8. TypeScript 2.0: Built-In Type Declarations
  9. TypeScript 2.1: async/await for ES3/ES5
  10. TypeScript 2.1: External Helpers Library
  11. TypeScript 2.1: Object Rest and Spread
  12. TypeScript 2.1: keyof and Lookup Types
  13. TypeScript 2.1: Mapped Types
  14. TypeScript 2.1: Improved Inference for Literal Types
  15. TypeScript 2.1: Literal Type Widening
  16. TypeScript 2.1: Untyped Imports
  17. TypeScript 2.2: The object Type
  18. TypeScript 2.2: Dotted Properties and String Index Signatures
  19. TypeScript 2.2: Null-Checking for Expression Operands
  20. TypeScript 2.2: Mixin Classes
  21. TypeScript 2.3: Generic Parameter Defaults
  22. TypeScript 2.3: The --strict Compiler Option
  23. TypeScript 2.3: Type-Checking JavaScript Files with --checkJs
  24. TypeScript 2.3: Downlevel Iteration for ES3/ES5
  25. TypeScript 2.4: String Enums
  26. TypeScript 2.4: Weak Type Detection
  27. TypeScript 2.4: Spelling Correction
  28. TypeScript 2.4: Dynamic import() Expressions
  29. TypeScript 2.5: Optional catch Binding
  30. TypeScript 2.6: JSX Fragment Syntax
  31. TypeScript 2.7: Numeric Separators
  32. TypeScript 2.7: Strict Property Initialization
  33. TypeScript 2.8: Per-File JSX Factories
  34. TypeScript 2.8: Conditional Types
  35. TypeScript 2.8: Mapped Type Modifiers