Migrate to Strict TypeScript Incrementally
Enable strict mode in an existing TypeScript project one flag at a time to avoid a massive migration and catch type errors gradually.
Prerequisites
- -Existing TypeScript project
- -Working build pipeline
Steps
Check current strictness level
Review your tsconfig.json to see which strict flags are already enabled.
Enable strict flags one at a time
Instead of enabling strict: true all at once, enable individual flags and fix errors incrementally.
Recommended order: noImplicitAny, strictNullChecks, strictFunctionTypes, strictBindCallApply, noImplicitThis, alwaysStrict.
Count errors for each strict flag
Measure the scope of each flag before committing to fix it.
Enable strictNullChecks and fix null errors
This is typically the most impactful flag. Fix undefined access patterns with optional chaining and nullish coalescing.
Use optional chaining (?.) and nullish coalescing (??) instead of non-null assertions (!) wherever possible.
Non-null assertions (!) silence the compiler but do not fix the underlying issue. Prefer proper null checks.
Enable full strict mode
Once all individual flags pass, enable strict: true to lock in all current and future strict checks.
After enabling strict: true, you can remove the individual flags since strict enables all of them.
Full Script
FAQ
Discussion
Loading comments...