Skip to the workspace
Pebellslearn by building
Type Inference  ›  Capstone Project

Compiler Options That Matter

Compiler Options 12 minA few compiler options decide most of TypeScript's behaviour: strict for how hard it checks, target and module for what JavaScript it writes, and noEmit or noEmitOnError for when it writes files.

typescript / Compiler Options That Matter

Explore each scene at your own pace.

01 · The rule

The rule

Compiler Options That Matter

A few compiler options decide most of TypeScript's behaviour: strict for how hard it checks, target and module for what JavaScript it writes, and noEmit or noEmitOnError for when it writes files.

Investigate how strictness, target and module settings change the compiler's decisions.

tsc --init lists well over a hundred options, and it is easy to feel you must understand them all. You do not. A handful decide almost everything about how your project is checked and what JavaScript comes out.

The rule

strict decides how hard it checks; target and module decide what it writes.

How hard it checks — strict. One switch that turns on a family of checks. The two you will feel most:

  • strictNullChecks — null and undefined are no longer silently allowed in every type. A function that might return nothing must say so, and callers must handle it.
  • noImplicitAny — a value whose type cannot be inferred, like an unannotated parameter, is an error instead of quietly becoming any.
function findTask(tasks: Task[], id: number) {
  return tasks.find(t => t.id === id); // Task | undefined
}
findTask(all, 3).title; // strict: error, the task may be undefined

Without strict mode that last line compiles and crashes at runtime when no task matches.

What it writes — target and module.

  • target — the JavaScript version to output, such as ES2022. With an older target, newer syntax is rewritten into older equivalents. It also picks a default set of built-in type declarations (lib), which decides whether methods like Array.prototype.at are known to exist.
  • module — the module system of the output, such as NodeNext for modern Node.js or ESNext for bundlers.

Whether it writes at all.

  • noEmit — check only, write no files. Common when a bundler or runner produces the JavaScript and tsc is used purely as a checker.
  • noEmitOnError — write nothing if there are type errors.
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.