Michael_Scharf Michael_Scharf - 18 days ago 5
TypeScript Question

How to add a typescript definition file to a npm package?

Is there a way to add typescript definitions (

.d.ts
files) to a pure javascript project directly (e.g. in
package.json
). I can't find any documentation on that.

Answer

There is a page in the TypeScript Handbook on adding typings to an NPM Package. I'll copy and paste here:

Typings for NPM Packages

The TypeScript compiler resolves Node module names by following the Node.js module resolution algorithm. TypeScript can also load typings that are bundled with npm packages. The compiler will try to discover typings for module "foo" using the following set of rules:

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"
}

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.

Your definition files should

  • be.d.ts files
  • be written as external modules
  • not contain 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.