Andreas Gassmann Andreas Gassmann - 1 year ago 139
Node.js Question

Writing NPM modules in Typescript

I am working on my first NPM module. I briefly worked with typescript before and a big problem was that for many modules there were no definition files available. So I thought it would be a good idea to write my module in typescript.

However, I can't find any information on the best way to do that. I found this related question "Can I write npm package in coffeescript?" where people suggest only publishing the javascript files. But in contrast to the coffeescript files, the typescript files might actually be useful if they are used within a typescript application.

Should I include Typescript files when publishing an NPM module, or should I only publish the javascript files and provide the generated .d.ts files to DefinitelyTyped?

Answer Source

Here is a sample Node module written in TypeScript :

Here is a sample TypeScript project that uses this sample module

Basically you need to :

  • compile with commonjs and declaration:true
  • generate a .d.ts file

And then

  • Have your ide read the generated .d.ts.

Atom-TypeScript just provides a nice workflow around this :