The TypeScript compiler
The TypeScript compiler takes programs written in TypeScript and converts them into JavaScript programs that web browsers can understand. It can also generate JavaScript that runs on your computer with Node.js. This process is called compilation.
This is useful because TypeScript can add a wide variety of helpful features like type checking, interfaces, and modern JavaScript syntax, while still producing a common output format that can be understood by any JavaScript engine.
Installing the compiler
You can install the TypeScript compiler with npm
.
npm install -g typescript
You can now compile an individual TypeScript file with tsc
.
tsc myFile.ts
Assuming there were no compilation errors, this will produce myFile.js
in the same directory.
Compiler configuration
Most TypeScript projects rely on a configuration file which instructs the TypeScript compiler to compile multiple files and directories. The configuration also allows you to change many options around how TypeScript interprets your files.
This configuration file is usually named tsconfig.json
. If you run tsc
without specifying an input file, it will search the current directory for a tsconfig.json
file, and follow the compilation instructions.
- TODO: interactive tsconfig generator
Typical development
If you are developing a TypeScript program, you may want the compiler to watch your source files for changes, and automatically recompile the JavaScript result. You can accomplish this with tsc --watch
.
tsc --watch
This will watch all input sources for changes, and incrementally build any files that need to be updated.