How To Compile TypeScript On The Command Line

TypeScript is a programing language that is a superset of JavaScript. It allows setting types and allows ES6 functionality. It is the preferred language of Angular2 and is maintained by Microsoft.

To compile TypeScript into JavaScript you need to have TypeScript installed. To install TypeScript using the Node Package Manager (npm) type the command:

npm install -g typescript

As long as you don't get any errors (warning are OK) you now have TypeScript installed onto your system. You can then use the 'tsc' command to transcribe TypeScript into Javascript.

TypeScript files normally have a '.ts' extension. To compile all the .ts files in the current directory, type:

tsc *.ts

this will output the JavaScript version of those files with a .js extension in that same directory.

You can also specify which files you want to compile:

tsc file1.ts file2.ts

You can also specify the output location or file for the new JavaScript files.

To output all the new JavaScript files into a directory called 'output' type:

tsc --outDir output *.ts

You can also concatinate all the specified files into a single JavaScript file. If you want the single JavaScript file to be called output.js you would type:

tsc --out output.js file1.ts file2.ts

Note that only global scripts support concatenation, modules don't.

Finally, you can specify a single output.js file in a output directory.

tsc --out output/output.js *.ts

To learn more, checkout my favorite TypeScript book.