Skip to content

Instantly share code, notes, and snippets.

@esfand
Last active November 19, 2015 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esfand/3bf620637039b00372b2 to your computer and use it in GitHub Desktop.
Save esfand/3bf620637039b00372b2 to your computer and use it in GitHub Desktop.

Typings for npm packages

https://github.com/Microsoft/TypeScript.wiki.git

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: microsoft/TypeScript#2338

What your typings file should be

  • your typings file should be a .d.ts file.
  • your typings file should be an external module.
  • your typings file should 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment