TypeScript at Scale
Turn on
strictin a codebase that was never written for it. Usesatisfiesinstead of an annotation. Know when a type is costing more than it returns.
In this chapter: the language against the engineering · migrating to strict one flag at a time · satisfies and what it fixes · module resolution and verbatimModuleSyntax · when to stop
💡 The Core Idea
Chapters 01 to 07 are the language. This one is the engineering: what changes when the codebase is large, old and shared. At that point every question is about cost rather than expressiveness. A more precise type catches more bugs and takes longer to check. A more clever type catches more bugs and takes longer for the next person to read. A senior answer is never "make it as precise as possible". It is knowing which errors a type buys, and at what price.
The other half is that a large codebase is almost never strict from the start. It gets there by migration. Interviewers reliably ask for a plan that is not "turn it on and fix 4,000 errors".
How It Works
strict is seven flags, and they go on one at a time
"strict": true enables a group. Turning the whole group on at once in an existing codebase produces thousands of errors and no path through them. Turning the flags on one by one produces a series of pieces of work you can finish.
| Flag | What it catches | Migration cost |
|---|---|---|
noImplicitAny | Parameters and members with no type | High — but it is where the value is |
strictNullChecks | null and undefined used as if they were not | Highest, and the most bugs found |
strictFunctionTypes | Unsound parameter variance in callbacks | Low |
strictBindCallApply | Wrong arguments through bind/call/apply | Low |
strictPropertyInitialization | Class fields never assigned | Low, and mostly in older code |
noImplicitThis | this of unknown type | Low |
useUnknownInCatchVariables | catch (e) treated as any | Low, and mechanical |
The order that works: the four low-cost flags first, because they are quick wins that build confidence. Then noImplicitAny. Then strictNullChecks last and on its own, because it is the one that surfaces real bugs and needs the attention.
For a codebase too large for even that, the escape hatch is strict: true plus per-file suppressions. But it has to come with a ratchet: a count that may fall and never rise. Add a CI check that counts the files carrying // @ts-nocheck or // @ts-expect-error and fails when the count goes up. A number that can only fall is what makes a migration finish. A suppression list with no gate only grows.
Two useful flags sit outside strict. noUncheckedIndexedAccess makes arr[i] yield T | undefined. That is the truth, but it is noisy in loop-heavy code. exactOptionalPropertyTypes separates "absent" from "present and undefined", and it breaks common spread patterns. Both are opt-in because the cost is real.
satisfies checks without widening
satisfies arrived in TypeScript 4.9 and changed how configuration objects are typed. Interviewers ask about it because the reason it exists is subtle.
type Route = { path: string; auth: boolean };
// ❌ An annotation checks the value, then throws the specifics away.
const routesAnnotated: Record<string, Route> = {
home: { path: "/", auth: false },
admin: { path: "/admin", auth: true },
};
routesAnnotated.hoem; // no error — the key type is `string`
// ✅ `satisfies` checks against the type and keeps the literal type.
const routes = {
home: { path: "/", auth: false },
admin: { path: "/admin", auth: true },
} satisfies Record<string, Route>;
routes.hoem; // Error: property does not exist
type RouteName = keyof typeof routes; // "home" | "admin"The distinction in one line: an annotation is a claim about what the variable is; satisfies is a check that the value conforms. With the annotation you get validation and lose the keys. With satisfies you get both, and keyof typeof becomes a usable union. That makes it the right tool for route tables, theme tokens, feature-flag maps and permission lists. The same argument applies against as const alone, which keeps the literals but checks nothing. as const satisfies T is a sound pairing when you want readonly as well.
Module resolution, and the flag that stops surprises
Module resolution is where a project that compiles locally fails in CI, or where an import works in the bundler and not in tsc.
moduleResolution | Use for |
|---|---|
bundler | Anything built by Vite, esbuild, webpack — matches what bundlers actually do |
nodenext | Code Node runs directly, where package.json exports and extensions are enforced |
node10 | Legacy only. It predates exports maps entirely |
verbatimModuleSyntax, added in TypeScript 5.0, is the one to be able to explain. It requires import type for anything used only as a type, then emits imports exactly as written. Without it the compiler decides which imports to elide (drop from the output). A value import that happens to be used only in a type position disappears, and its side effects go with it. The flag moves that erasure into the source, where it is visible in review.
skipLibCheck and project references belong to the build-time discussion in Type-Checking and Linting at Scale. That chapter explains why type checking does not parallelise and what a project graph does about it.
Declaration files, when you have to write one
Most .d.ts files you write are for an untyped dependency or a non-code import.
// types/untyped-lib.d.ts — a minimal, honest shim
declare module "legacy-charts" {
export interface ChartOptions {
data: number[];
labels?: string[];
}
export function render(el: HTMLElement, options: ChartOptions): void;
}The rule for a shim: type only what you call, and type it accurately. A shim that declares the whole library from guesswork is worse than no types, because the compiler now confidently agrees with something wrong. any in one place you have not verified is more honest than an invented signature. A wildcard module, such as declare module "*.svg" exporting a string, applies the same rule to assets.
When types start costing more than they return
Type-level computation is real computation. It is single-threaded, and it re-runs on every check.
// ❌ A recursive conditional over a large union: compiles, and slowly.
type DeepPaths<T, Prefix extends string = ""> = {
[K in keyof T & string]: T[K] extends object
? DeepPaths<T[K], `${Prefix}${K}.`> | `${Prefix}${K}`
: `${Prefix}${K}`;
}[keyof T & string];
// ✅ Generate the union from the runtime source instead, or accept `string`.Three signals show that a type has gone past its value, and each one has a cheaper answer:
| Signal | The cheaper answer |
|---|---|
| Editor autocomplete takes seconds in one file | Simplify the type, or split the file |
| Only one person on the team can modify it | A validated runtime schema, with the type inferred from it |
| The error message is forty lines of nested conditionals | A narrower type and a runtime check at the boundary |
The last row is the general escape. A schema validator gives you a runtime check and an inferred type from one declaration. That usually beats proving the same thing in the type system. Use tsc --generateTrace when you need evidence about which type is slow rather than a hunch.
⚠️ Moving target: TypeScript adds and renames compiler flags every few releases, and the native port changes the performance arithmetic behind this section. The lasting principle holds: type-level work is single-threaded and re-runs on every check, so precision has a measurable price.
When to Use It
| Situation | Do | Why |
|---|---|---|
A large codebase not on strict | The four cheap flags, then noImplicitAny, then strictNullChecks | Each step finishes; the group does not |
| A migration that has to run for months | strict plus a counted opt-out list that can only shrink | Without the ratchet the list is permanent |
| A route table, theme or flag map | satisfies | Validation and the literal keys |
| A discriminated union you want narrowed everywhere | An annotation | You want the wide type here |
| Data crossing a network or file boundary | A runtime schema with the type inferred | The compiler cannot check what it did not see |
| An untyped dependency | A shim covering only what you call | An invented signature is worse than any |
| A type nobody else can edit | Delete it and validate at runtime | The maintenance cost exceeded the bugs caught |
Common Mistakes
❌ Turning strict: true on across a large codebase in one commit. Thousands of errors, no route through them, and the branch is abandoned. ✅ One flag at a time, cheapest first, strictNullChecks last and alone.
❌ A @ts-nocheck suppression list with no gate. It only ever grows. ✅ Count the suppressions in CI and fail when the number rises.
❌ Annotating a configuration object with Record<string, T>. The keys widen to string and typos stop being errors. ✅ satisfies, which checks conformance and keeps the literal keys.
❌ A hand-written .d.ts covering a whole untyped library. The compiler now vouches for guesses. ✅ Declare only the surface you call; leave the rest genuinely unknown.
❌ Proving an invariant in the type system when the data comes from the network. Types are erased, so the guarantee does not exist at runtime. ✅ Validate at the boundary and infer the type from the schema.
🔑 Key Takeaways
strictis seven flags, and an existing codebase migrates by turning them on one at a time.- A migration needs a suppression count that can only fall; without a gate the opt-out list is permanent.
- An annotation is a claim about the variable and widens it;
satisfiesis a check that keeps the literal type. verbatimModuleSyntaxmakes type-only erasure explicit in the source rather than a compiler decision.- Type-level computation is single-threaded and re-run every check, so a clever type has a measurable price.
Interview Questions
Q: How would you get a 200,000-line codebase onto strict?
Not in one commit. strict is seven flags, so I would turn on the four cheap ones first: strictFunctionTypes, strictBindCallApply, noImplicitThis and useUnknownInCatchVariables. Then noImplicitAny, then strictNullChecks on its own, because that is the one that surfaces real bugs. If even that is too large, I would use strict: true with per-file suppressions, plus a CI check that the count only falls. The gate is the important part. Without it the exclusion list becomes permanent.
Q: What does satisfies do that a type annotation does not?
It checks the value against the type without widening it. An annotation says what the variable is, so Record<string, Route> validates the entries and then throws away the specific keys. A typo in a lookup stops being an error. satisfies validates the same way and keeps the literal type, so keyof typeof gives a real union of the keys. That is why it fits route tables and permission maps.
Q: When have types become too expensive?
When the editor gets slow in one file, when only one person can safely modify a type, or when a mismatch produces a forty-line error nobody can read. All three say the type has moved past what it buys. The usual replacement is a runtime schema. One declaration gives both a runtime check and an inferred type. That is safer for anything crossing a boundary, and much cheaper to maintain.
What to Read Next
- TypeScript Advanced and Utility Types — the type-level features whose cost this chapter prices
- Type-Checking and Linting at Scale — project references and keeping whole-program checks off the critical path
- React and TypeScript at Scale — the same discipline applied to props, state and the server boundary