Turning on TypeScript strict mode in an existing app
Short answer: TypeScript's strict option switches on a family of stricter checks, most importantly strictNullChecks and noImplicitAny, which catch the "cannot read property of undefined" crashes customers actually hit. In an existing app, don't flip it on and face thousands of errors at once. Turn on individual strict flags one at a time, fix or explicitly mark the errors, stop new code adding more, and switch on full strict once the remaining flags are clean.
Why strict mode is worth it
Without strict checks, TypeScript lets plenty of risky code through. The classic example:
const customer = customers.find(c => c.id === id);
sendEmail(customer.email);
find can return undefined. Without strictNullChecks, TypeScript doesn't complain, and the app crashes at runtime for the one customer ID that doesn't exist. With it, the compiler points at the line and asks you to handle the missing case.
For a business app, these are the bugs that show up as a blank screen at checkout, a failed invoice run or a broken admin page. Strict mode moves them from production to your editor.
What strict includes
The TypeScript configuration reference describes strict as enabling a wide range of type-checking behaviour for stronger guarantees of program correctness. It's shorthand for a family of options, which the reference lists, including:
noImplicitAny: errors where a type would silently becomeany.strictNullChecks: treatsnullandundefinedas separate types you must handle.strictFunctionTypes: checks function parameter types more accurately.strictPropertyInitialization: requires class properties to be initialised.strictBindCallApply,noImplicitThis,alwaysStrict,useUnknownInCatchVariablesand others.
The reference also warns that future versions of TypeScript may add more checks under the strict flag, so upgrading TypeScript can surface new errors. Read release notes when you upgrade.
Why not just turn it all on?
In a codebase that has grown without strict checks, switching on strict can produce hundreds or thousands of errors. Fixing them all in one branch means a huge, risky change that's hard to review and blocks other work for days. Teams that try it often give up and switch it off again.
The reference documents the way out: you can turn individual checks on and off. That makes a gradual migration practical.
A step-by-step migration
1. Measure
Run the compiler with each flag switched on temporarily and count errors per flag. That tells you where the work is. Typically strictNullChecks produces the most errors and the most value; others, like alwaysStrict, produce few.
2. Start with the cheap flags
Switch on flags with few errors first, fix them and merge. Quick wins build confidence and shrink the list.
3. Tackle noImplicitAny
Add explicit types where TypeScript was guessing any. For third-party libraries without types, install their type packages or write a small declaration file. Where a proper type isn't worth it yet, an explicit any with a short comment is better than an implicit one, because it's visible and searchable.
4. Tackle strictNullChecks gradually
This is the big one. Options for pacing it:
- Folder by folder: some teams use a separate config for already-converted folders, compiled in CI alongside the main one.
- With tooling: community tools exist that track which files pass strict checks and stop regressions.
- Error budget: record the current error count in CI and fail if it goes up, so new code can't add errors while old ones are fixed.
When fixing, handle the missing case properly: show a "not found" message, return early or provide a sensible default. Avoid sprinkling the non-null assertion ! everywhere; it silences the compiler without making the code safer.
5. Switch on strict
Once each individual flag is clean, replace them with "strict": true in tsconfig.json. From then on, new strict checks in future TypeScript versions apply automatically.
Patterns that make strict code pleasant
- Narrow early. Check for
undefinedat the top of a function and return, so the rest of the function works with a definite value. - Use optional chaining and nullish coalescing:
customer?.email ?? "no email"handles missing values concisely. - Type API responses. Validate data from APIs and forms at the boundary, so the rest of the code can trust its types.
- Treat caught errors as unknown. With
useUnknownInCatchVariables, check what an error is before reading its properties.
Common errors you'll meet
A few error types make up most of the work in a typical migration:
- "Object is possibly undefined" after array lookups,
findcalls or optional API fields. Handle the missing case or narrow with a check. - "Parameter implicitly has an any type" in callbacks and event handlers. Add the parameter type, often copied from the library's own types.
- "Property has no initializer" in classes. Initialise it in the constructor, give it a default, or mark it optional if it really can be missing.
- Errors in tests and scripts. Decide whether test files follow the same rules; many teams include them, because tests are code too.
Fixing these one category at a time, across the codebase, is often faster than going file by file.
Keep CI in charge
Run tsc --noEmit in CI on every pull request, with the same configuration developers use. Fast type checks in the editor give immediate feedback, but CI makes sure nothing slips through.
A worked example
Picture a hypothetical Nelson outdoor-gear retailer with a TypeScript storefront and admin panel built without strict checks. Runtime errors from undefined values appeared in error tracking every week. The team counted errors per strict flag, enabled the smaller ones first, added explicit types for a few untyped libraries, then worked through strictNullChecks one folder at a time with a CI check that blocked the error count from rising. Over a couple of months they reached full strict, and that class of runtime error became rare.
FAQ
Does strict mode slow down development?
Slightly at first, while people learn the patterns. It usually saves time overall by preventing bugs and making refactoring safer.
Will strict mode change how my app runs?
No. Type checking happens at compile time. alwaysStrict adds "use strict" to output, which existing code rarely notices.
Should new projects start strict?
Yes. Starting strict costs almost nothing; converting later is the expensive path.
If your TypeScript app keeps throwing undefined errors in production, ask us to plan a strict-mode migration.