tsc CLI
Compile files
Start with one file. Pass the file name to tsc and it writes JavaScript next to the source. Open the emitted file once so the mapping from TypeScript to JavaScript feels concrete.
A clean compile prints nothing. Errors print with file name, line, and column, so read the first error and fix from the top down.
tsc app.ts
function add(a: number, b: number): number { return a + b; } const total = add(2, 3); console.log(total);
Watch mode
Watch mode keeps tsc running and recompiles on every save. It is the tight feedback loop you want while learning and while building a small feature.
Leave the terminal visible beside the editor. When an error appears, fix it, save again, and confirm that the watcher goes quiet.
tsc --watch
8:36:32 PM - Starting compilation in watch mode...
8:36:38 PM - Found 0 errors. Watching for file changes.
The time prefix is local to the machine; the rest of each line is what tsc prints.
Use a project
A project file named tsconfig.json stores shared settings for the whole folder. Point tsc at the project and it compiles every included file with the same rules.
This is how real codebases keep builds consistent. One developer command builds the same output on every machine.
tsc --project tsconfig.json
A clean build prints nothing, exactly like the single file command.
Check without emitting
Sometimes you only want answers, not files. Ask tsc to type check and skip emit entirely. Nothing is written, so the folder stays clean.
Editors run the same check in the background, so the command line and the editor agree. Run it before a commit to catch what tired eyes miss.
tsc --noEmit
When the types are clean the command prints nothing; an error appears with its file name, line, and column.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
tsc app.tswrite on a clean compile?Answer
A JavaScript file next to the source
The compiler emits app.js beside app.ts and prints nothing when the types are clean.
-
What does
tsc --watchdo?Answer
It keeps running and recompiles after each save
The watcher stays open, rebuilds on changes, and prints the result of each build.
-
What does
tsc --project tsconfig.jsonuse?Answer
The settings of the named project file for every included file
The project file holds the compiler options and the file list, so every file builds the same way.
-
What does
tsc --noEmitdo?Answer
It type checks and writes no JavaScript files
The check still runs, but nothing is written, so the folder stays clean.
-
What does this program print?
function add(a: number, b: number): number { return a + b; } console.log(add(2, 3));Answer
5
Both arguments are numbers, so the plus adds them and the log prints 5.
-
What does this program print?
const total = "2" + 3; console.log(total);Answer
23
The plus with a string joins the two values into the text 23.
-
What does this program print?
let n = 1; n = n + 1; console.log(n);Answer
2
The variable is reassigned to its value plus one and then printed.
-
What does this program print?
const values = [1, 2, 3]; console.log(values.length);Answer
3
The array holds three numbers, so its length is 3.
-
What does the compiler say about this program?
const n: number = "x"; console.log(n);Answer
error TS2322: Type 'string' is not assignable to type 'number'.
A string literal cannot be stored in a variable typed as number.
-
What does the compiler say about this program?
function add(a: number, b: number): number { return a + b; } console.log(add("2", 3));Answer
error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
The first parameter is a number, and the string argument does not fit it.
Article author: Arthur Isaev