TypeScript with NodeJS
TypeScript has had first class support for NodeJS since inception. Here's how to get setup with a NodeJS project in TypeScript:
- Compile with
--moduleset to"commonjs"(as we mentioned in modules) - Add
node.d.ts(typings install dt~node --global) to your compilation context.
That's it! Now you can use all the built in node modules (e.g. import fs = require('fs')) with all the safety and developer ergonomics of TypeScript!
Creating TypeScript node modules
You can even use other node modules written in TypeScript. As a module author, one real thing you should do:
- you might want to have a
typingsfield (e.g.src/index) in yourpackage.jsonsimilar to themainfield to point to the default TypeScript definition export. For an example look atpackage.jsonfor csx.
Example package: npm install csx for csx, usage: import csx = require('csx').