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

The following statement:<br />

import foo = require('foo');<br />

actually imports two things:<br />

The type information from the imported file.<br />

Takes are runtime dependency on the foo module.<br />

You can pick and choose so that only the type information is loaded and no runtime dependency occurs. Before continuing<br />

you might want to recap the declaration spaces section of the book.<br />

If you do not use the imported name in the variable declaration space then the import is completely removed from the<br />

generated JavaScript. This is best explained with examples. Once you understand this we will present you with use cases.<br />

Example 1<br />

import foo = require('foo');<br />

will generate the JavaScript:<br />

Thats right. An empty file as foo is not used.<br />

Example 2<br />

import foo = require('foo');<br />

var bar: foo;<br />

will generate the JavaScript:<br />

var bar;<br />

This is because foo (or any of its properties e.g. foo.bas ) is never used as a variable.<br />

Example 3<br />

import foo = require('foo');<br />

var bar = foo;<br />

will generate the JavaScript (assuming commonjs):<br />

var foo = require('foo');<br />

var bar = foo;<br />

File Module Details<br />

44

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

Saved successfully!

Ooh no, something went wrong!