Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 610
Andreas Gassmann Avatar asked Jun 19 '15 00:06

Andreas Gassmann


People also ask

Can you use NPM packages in TypeScript?

Globally Installing TypeScript:You can use npm to install TypeScript globally, this means you can use the tsc command anywhere in your terminal. You can also use use npx when you have to run tsc.


1 Answers

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

like image 95
basarat Avatar answered Sep 19 '22 17:09

basarat