TypeScript Compiler (tsc)
In this page:
What tsc Does
The tsc command is the main command-line compiler provided by the TypeScript package. It reads your .ts source files, performs full type checking against your code, and can emit plain JavaScript output ready to run anywhere JavaScript runs.
Example: What tsc Does
let total: number = 5 + 10;
console.log(total);
// tsc type-checks this file, then emits plain JavaScript
Compiling a Single File
You can give a specific file name to tsc to compile just that file, for example tsc app.ts. By default this creates a matching app.js file right next to the source, translating the TypeScript syntax into equivalent JavaScript.
Example: Compiling a Single File
// Run in a terminal (not executable here):
// tsc app.ts
// creates app.js next to app.ts
console.log("Compiled with: tsc app.ts");
Type Checking
The compiler checks whether your code follows the declared type rules across your entire program, not just syntax errors. Compiler errors surface problems like calling a function with the wrong argument types before the generated program ever executes.
Example: Type Checking
function multiply(a: number, b: number): number {
return a * b;
}
// multiply(2, "3"); // compiler error: wrong argument type
console.log(multiply(2, 3));
Using Compiler Options
The compiler supports many options for controlling how strictly types are checked and how the JavaScript output is generated, such as target language version or module format. These options can be passed directly on the command line or, more commonly, stored centrally in a tsconfig.json file.
Example: Using Compiler Options
// Run in a terminal (not executable here):
// tsc app.ts --target ES2015 --module commonjs
console.log("Compiler options control target JS version and module format");
Compiling a Project
For larger projects, running tsc with no file name tells it to look for a tsconfig.json file and compile the whole project according to its settings. This is the standard workflow once a project grows beyond a single file.
Example: Compiling a Project
// Run in a terminal (not executable here):
// tsc
// compiles the whole project using tsconfig.json
console.log("Running tsc with no filename uses tsconfig.json");
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: