11.09.2015 Views

Typescript Deep Dive by Basarat Ali Syed

Typescript Deep Dive by Basarat Ali Syed

Typescript Deep Dive by Basarat Ali Syed

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

TypeScript <strong>Deep</strong> <strong>Dive</strong><br />

TypeScript with NodeJS<br />

TypeScript has had first class support for NodeJS since inception. Here's how to get setup with a NodeJS project in<br />

TypeScript:<br />

1. Compile with --module set to "commonjs" (as we mentioned in modules)<br />

2. Compile with --declaration set to true . This gets TypeScript to generate a .d.ts file which is what TypeScript looks<br />

for in your node_modules for using TypeScript NPM modules from other TypeScript modules.<br />

3. Add node.d.ts ( tsd install node ) to your compilation context.<br />

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<br />

ergonomics of TypeScript!<br />

You can even use other node modules written in TypeScript (e.g. npm install csx for csx usage: import csx =<br />

require('csx') ). One trick here is that you might want to have a typings field (e.g. src/index ) in your package.json similar<br />

to the main field to point to the default TypeScript definition export.<br />

NodeJS<br />

68

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!