Lesson 28 / 28

Strict Mode & Compiler Errors

Turn on strict mode, handle null and undefined explicitly, and read TypeScript compiler errors to find the fix fast.

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: 's' 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
  • Disables type checking in test files
Answer

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

Final quiz 1 of 8

Final quiz

Quick check: Which `tsconfig.json` option enables the full set of strict checks?

  • noEmit
  • strict
  • target
  • outDir
Answer

strict — `strict: true` bundles strictNullChecks, noImplicitAny and more.

Final quiz 2 of 8

Final quiz

Quick check: Which type should you prefer over `any` for unpredictable data?

  • unknown
  • object
  • Function
  • never
Answer

unknown — `unknown` forces you to narrow before use.

Final quiz 3 of 8

Final quiz

Quick check: What does a tuple like `[string, number]` enforce?

  • Any length of mixed values
  • A fixed length with a type at each position
  • Only string values
  • Readonly access
Answer

A fixed length with a type at each position — Tuples fix both length and per-position types.

Final quiz 4 of 8

Final quiz

Quick check: How do you let TypeScript narrow a union by a shared field?

  • Use a discriminated union with a literal `kind`
  • Cast with `as any`
  • Use `Partial<T>`
  • Declare the field optional
Answer

Use a discriminated union with a literal `kind` — A literal discriminant lets `switch` narrow each variant.

Final quiz 5 of 8

Final quiz

Quick check: What does `Partial<User>` produce?

  • A type with every property optional
  • A type with every property readonly
  • A type with only required keys
  • A union of keys
Answer

A type with every property optional — `Partial<T>` makes all properties optional.

Final quiz 6 of 8

Final quiz

Quick check: What does `T extends { length: number }` do?

  • Constrains T to types that have a length property
  • Makes T a number
  • Adds a length field at runtime
  • Disables inference
Answer

Constrains T to types that have a length property — Constraints narrow what a generic accepts while staying reusable.

Final quiz 7 of 8

Final quiz

Quick check: What does `import type { Point }` do to the output JavaScript?

  • Adds a runtime import
  • Is erased at compile time
  • Bundles the type
  • Creates a class
Answer

Is erased at compile time — Type-only imports are removed from the emitted JS.

Final quiz 8 of 8

Final quiz

Quick check: What type does `await` give you for `Promise<User>`?

  • Promise<User>
  • User
  • unknown
  • void
Answer

User — `await` unwraps the Promise to its resolved type.