Lesson 26 / 26

Strict Mode & Compiler Errors

One flag that turns on TypeScript's full safety net, and how to read what it tells you.

The strict flag

"strict": true in tsconfig.json bundles several checks — strictNullChecks, noImplicitAny, and more — into one switch. New projects should turn it on from day one.

strictNullChecks catch

With strict null checks, null/undefined aren't silently allowed everywhere — you must handle them explicitly.

function getLength(s: string | null): number {
  return s.length;
  // Error: Object is possibly 'null'.
}

function getLengthSafe(s: string | null): number {
  return s ? s.length : 0; // narrowed, OK
}

Reading compiler errors

TS error messages name the exact incompatible types ("Type 'X' is not assignable to type 'Y'") — read the two type names first; they usually point straight at the fix.

Quick check: What does `"strict": true` in tsconfig.json do?

  • Enables a single extra check
  • Bundles several strict type-checking options together
  • Only affects the output file names
Answer

Bundles several strict type-checking options together — strict is a shorthand that turns on strictNullChecks, noImplicitAny, and several other checks at once.