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

Variables<br />

For example to tell TypeScript about the process variable you can do:<br />

declare var process:any;<br />

You don't need to do this for process as there is already a community maintained node.ts<br />

This allows you to use the process variable without TypeScript complaining:<br />

process.exit()<br />

We recommend using an interface wherever possible e.g:<br />

interface Process {<br />

exit(code?:number):void;<br />

}<br />

declare var process: Process;<br />

This allows other people to extend the nature of these global variables while still telling TypeScript about such<br />

modifications. E.g. consider the following case where we add an exitWithLogging function to process for our amusement:<br />

interface Process {<br />

exitWithLogging(code?:number):void;<br />

}<br />

process.exitWithLogging = function() {<br />

console.log("exiting");<br />

process.exit.apply(process,arguments);<br />

}<br />

Lets look at interfaces in a bit more detail next.<br />

Variables<br />

59

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

Saved successfully!

Ooh no, something went wrong!