APPENDIX 2

image

TypeScript Compiler

The TypeScript compiler may well be hidden behind your development tools, but it is worth familiarizing yourself with the various compiler options. The compiler itself is optimized for compiling entire projects, rather than individual files. Running the compiler against the whole project means that you are only loading the standard library once, rather than many times. You may find that running compilation for a single file takes nearly as long as compiling the entire project.

The compiler is called tsc.exe and is usually found in one of the following directories on Windows:

  • C:Program Files (x86)Microsoft SDKsTypeScript
  • C:Program FilesMicrosoft SDKsTypeScript

To run the compiler, simply call tsc from the command line passing in your starting file name, as demonstrated below. You may need to enter the full address of the tsc.exe file. If you get bored of entering the full path, you can add the path to the TypeScript folder to your environment variables.

tsc app.ts

You can also run the TypeScript compiler using NodeJS on any operating system, as the compiler is written in TypeScript and compiled to JavaScript, as shown here. You will need to enter the full path to the tsc.js file (not to the .ts file).

node tsc.js app.ts

Getting Help

If you ever have trouble remembering all of the options for the compiler, you can get help using one of the following commands, which will display a list of all the available compiler flags you can set. Each compiler flag exposes a setting that allows you to change how the compiler behaves.

tsc --help
node tsc.js

Common Flags

There are a few compiler flags that are so common you are almost certain to use them at some point. These flags are described in the following sections.

Module Kind

The module compiler flag is used to generate code that loads external modules using either CommonJS or AMD module patterns. The valid values for the module kind are commonjs and amd.

tsc --module amd app.ts

ECMAScript Target Version

The target flag allows you to set the target ECMAScript version. Currently you can target versions 3 and 5 of the ECMAScript specification, but version 6 will be available once the ECMAScript 6 specification is stable. You currently specify the version using ES3 or ES5.

If you are targeting ECMAScript 3, you can’t use properties in your TypeScript program as they rely on an ECMAScript 5 feature. When the ECMAScript 6 option is added, it will perform fewer transformations during compilation. This is because ECMAScript 6 supports many of the basic TypeScript features, such as modules, classes, arrow functions, and special parameter types. The ECMAScript 6 support should also allow the new JavaScript features such as let, generators, and destructors.

tsc --target ES5 app.ts

Generate Declarations

The declaration flag will generate an additional file with the suffix .d.ts, which will contain ambient declarations for your code.

tsc --declaration app.ts

Remove Comments

The removeComments flag erases all comments from the output, which will make it smaller if you have a lot of comments in your source code.

tsc --removeComments app.ts

Combined Output

You can combine your entire TypeScript program into a single output file using the out compiler flag. When you use the out flag you must also supply a name for the combined file.

tsc --out final.js app.ts

No Implicit Any

The noImplicitAny flag disallows the compiler from inferring the any type. You can still explicitly annotate items with the any type, but if the compiler attempts to infer a type and can’t, it will generate an error. The beauty of this flag is that it lets you write less cluttered code because you can leave out type annotations except where the compiler warns you it can’t infer the type.

tsc --module noImplicitAny app.ts
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.213.44