Skip to article content
FrontendXLesson 21 / 40

Migrating to strict mode

Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.

advanced · about 45 minutes · 3 exercises

  • Predict
  • Implement
  • Debug

What you will be able to do

#
  • List what the strict family enables.
  • Migrate a file or package incrementally without breaking the build.
  • Use tsconfig overrides to tighten one area at a time.

Overview

#

What strict turns on

strict: true enables strictNullChecks, strictFunctionTypes, strictBindCallApply, strictPropertyInitialization, noImplicitAny, noImplicitThis, alwaysStrict, useUnknownInCatchVariables and, since TypeScript 5.6, strictBuiltinIteratorReturn.

Incremental strategy

  1. Turn on strict for new files first.
  2. Fix strictNullChecks errors, which are usually the majority.
  3. Add explicit types at boundaries; avoid any.
  4. Use unknown in catch and narrow.

Per-directory config

A nested tsconfig.json can extend the root and override flags for one folder, so you migrate a package at a time.

snippet.json
{ "extends": "../../tsconfig.json", "compilerOptions": { "strict": true } }

Common fixes

  • Replace any with unknown plus a guard.
  • Add | undefined to optional fields.
  • Initialize class fields or use ! only when a framework guarantees assignment.
  • Handle null from DOM queries before use.

Do not fake it

// @ts-expect-error and as any silence errors without fixing them. Use them only with a comment explaining why, and track them.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    With strictNullChecks on, does this compile? ```ts function len(s: string) { return s.length; } len(null); ```

    Choose the best answer
  2. Exercise 2Implement

    Rewrite this to be strict-safe without `any`, using `unknown` and a guard. ```ts function getLength(value: any) { return value.length; } ```

    starter.typescript
    function getLength(value: any) { return value.length; }
    

    Reveal the reference answer
    reference.typescript
    function getLength(value: unknown): number {
      return typeof value === "string" ? value.length : 0;
    }
    

    Authored expected output: Compiles with no errors. Runtime: getLength("abc") is 3, getLength(1) is 0.

    unknown forces a narrowing branch before the value is used.

  3. Exercise 3Debug

    This fails under strictPropertyInitialization. Fix it without disabling the flag. ```ts class Session { token: string; } ```

    starter.typescript
    class Session { token: string; }
    

    Reveal the reference answer
    reference.typescript
    class Session {
      token: string;
      constructor(token: string) {
        this.token = token;
      }
    }
    

    Authored expected output: Compile error resolved: Property 'token' has no initializer. Assigning it in the constructor fixes it.

    strictPropertyInitialization requires every field to be definitely assigned.

Common misconceptions

#
  • strict is a single flag with no sub-flags.
  • Turning on strict requires a big-bang rewrite.
  • ! is safer than a runtime check.

Follow-up practice

#
  • typescript-branded-types

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.