Andreas Gassmann Andreas Gassmann - 5 months ago 64
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

Here is a sample Node module written in TypeScript : https://github.com/basarat/ts-npm-module

Here is a sample TypeScript project that uses this sample module https://github.com/basarat/ts-npm-module-consume

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 : https://github.com/TypeStrong/atom-typescript#packagejson-support