Skip to content

Typings for npm packages

Daniel Rosenwasser edited this page Oct 21, 2015 · 12 revisions

TypeScript 1.6 has introduced new way of resolving module names that mimics the Node.js module resolution algorithm. This means the TypeScript compiler can currently load typings that are bundled with npm packages. The compiler will try to discover typings for module "foo" using the following set of rules:

  1. Try to load the package.json file located in the appropriate package folder (node_modules/foo/). If present,read the path to the typings file described in the "typings" field. For example, in the following package.json, the compiler will resolve the typings at node_modules/foo/lib/foo.d.ts

    {
        "name": "foo",
        "author": "Vandelay Industries",
        "version": "1.0.0",
        "main": "./lib/foo.js",
        "typings": "./lib/foo.d.ts"
    }
  2. Try to load a file named index.d.ts located in the package folder (node_modules/foo/) - this file should contain typings for the package.

The precise algorithm for module resolution can be found here

What your typings file should

  • be a .d.ts file
  • be an external module
  • not have triple-slash references

The rationale is that typings should not bring new compilable items to the set of compiled files; otherwise actual implementation files in the package can be overwritten during compilation. Additionally, loading typings should not pollute global scope by bringing potentially conflicting entries from different version of the same library

Clone this wiki locally