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.
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
- Turn on strict for new files first.
- Fix strictNullChecks errors, which are usually the majority.
- Add explicit types at boundaries; avoid
any. - Use
unknownin 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.
{ "extends": "../../tsconfig.json", "compilerOptions": { "strict": true } }
Common fixes
- Replace
anywithunknownplus a guard. - Add
| undefinedto optional fields. - Initialize class fields or use
!only when a framework guarantees assignment. - Handle
nullfrom 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.
Exercise 1Predict
With strictNullChecks on, does this compile? ```ts function len(s: string) { return s.length; } len(null); ```
Exercise 2Implement
Rewrite this to be strict-safe without `any`, using `unknown` and a guard. ```ts function getLength(value: any) { return value.length; } ```
starter.typescriptfunction getLength(value: any) { return value.length; }Reveal the reference answer
reference.typescriptfunction 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.
Exercise 3Debug
This fails under strictPropertyInitialization. Fix it without disabling the flag. ```ts class Session { token: string; } ```
starter.typescriptclass Session { token: string; }Reveal the reference answer
reference.typescriptclass 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
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.