TypeScript · Lesson 3 of 42
How the TypeScript compiler works: checking and emitting
Understand the two jobs of tsc, type checking and emitting JavaScript, why types are erased, and how the target option changes the output.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will know what happens between saving a .ts file and running the result, which explains many things that confuse beginners: why code with errors can still run, why you cannot inspect a type at run time, and why the target setting exists.
Two jobs in one command
When you run tsc, it does two things that are easy to mix up:
- Type checking. It reads your files, works out the type of every value and reports places where they do not fit.
- Emitting. It writes JavaScript files (and optionally other outputs) from your TypeScript.
The second job is mostly the first job's source with the types stripped out. Look at a small program and what the compiler writes for it. The file is greet.ts:
type User = { name: string; age: number };
function describe(user: User): string {
return `${user.name} is ${user.age}`;
}
console.log(describe({ name: "Asha", age: 30 }));Asha is 30Compiling that file with the default options produces this JavaScript:
"use strict";
function describe(user) {
return `${user.name} is ${user.age}`;
}
console.log(describe({ name: "Asha", age: 30 }));The type User line has disappeared, and so have : User and : string. This is called type erasure. It also tells you something important: the type User does not exist when the program runs, so you cannot write code that asks "is this value a User?" by looking at the type. You can only look at the value itself, which later lessons show how to do safely.
Errors do not have to stop the output
Because checking and emitting are separate, code with a type error can still produce JavaScript. By default tsc prints the error, writes the file anyway, and exits with a non-zero code to signal a problem. This surprises people, because it means a build can "succeed" in producing files while reporting errors. Two settings change that behaviour:
noEmitOnError: trueintsconfig.jsonmakes the compiler write nothing when there are errors.--noEmitmakes the compiler check only and never write files. Many projects use another tool to turn TypeScript into JavaScript and runtsc --noEmitseparately as the type check.
The target option
JavaScript keeps getting new syntax. The target option tells the compiler which version of JavaScript to write. With a modern target, features such as template strings and arrow functions are left as they are. With an old target, the compiler rewrites them using older syntax. Choose a target that matches where the code will run: a recent Node.js version supports modern syntax, while code that must run in very old browsers needs an older target.
The module option plays a similar role for imports and exports: it chooses whether the output uses ES modules or the older CommonJS format. A later lesson covers both settings in tsconfig.json.
Checking without writing, and watching
Two flags save a lot of time:
npx tsc --noEmit
npx tsc --watchThe first runs the checker and writes nothing, which is a quick way to ask "does everything still type-check?". The second keeps tsc running and recompiles every time you save a file, showing new errors within a second or two. Press Ctrl+C to stop it.
What the checker can and cannot know
The checker works on your source code only. It does not run your program. It reasons about what types could be at each point, using the annotations you wrote and the types it can work out itself. That is why it can say "this might be undefined" without ever executing the line. It also means it cannot know what a server will send tomorrow, which is why data from outside needs extra care.
Common mistakes
Assuming no output means a perfect build. With the default settings, files are written even when there are errors. Read the error list, and consider noEmitOnError.
Looking for type information at run time. typeof in JavaScript tells you about values, not about your TypeScript types. A type alias is gone after compilation.
Choosing a target without knowing where the code runs. A target that is too old produces larger, slower code. A target that is too new may use syntax an older environment cannot read.
Forgetting to recompile. If the .js file looks out of date, you probably changed the .ts file and did not compile again. Use --watch while you work.
Exercise
Create greet.ts from this lesson. Compile it and open greet.js to see what remains. Add a deliberate type error, such as calling describe with a string, and compile again. Check three things: does tsc print an error, does greet.js still change, and what exit code does echo $? show on macOS or Linux? Then add "noEmitOnError": true to tsconfig.json, repeat, and note what is different.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.