TypeScript ยท Chapter 3 of 44

The TypeScript Compiler (tsc)

The TypeScript compiler, called `tsc`, is the tool that turns your .ts files into plain JavaScript. It reads your code, checks the types, reports any errors, and then emits .js output files.

You can compile a single file directly, or set up a project with a configuration file so `tsc` knows exactly which files to include and how to compile them.

Syntax
tsc app.ts
tsc app.ts --watch

Compiling a single file

Running `tsc app.ts` compiles app.ts into app.js in the same folder. If there are type errors, tsc prints them to the terminal but still creates the JavaScript file by default.

Watch mode

Adding the `--watch` flag makes tsc keep running and automatically recompile whenever you save a file, which is very useful during development.

Example 1 (bash)
tsc app.ts
Output
app.js created

Compiles app.ts into a plain JavaScript file named app.js.

Example 2 (bash)
node app.js
Output
Hello, World!

Runs the compiled JavaScript file with Node.js.

Key points

  • `tsc` is the official TypeScript compiler.
  • It converts .ts files into .js files.
  • Type errors are reported, but JavaScript output is still produced by default.
  • `tsc --watch` recompiles automatically on file changes.
๐Ÿ’ก Note: You can stop tsc from emitting JavaScript when there are errors by setting `noEmitOnError: true` in tsconfig.json.

๐Ÿ“ Quick Quiz

1. What does the `tsc` command do?

2. What flag makes tsc automatically recompile on save?

3. By default, does tsc still create a .js file if there are type errors?