Sub topic 15
Moving an Existing Project to Strict Mode
Assume you know basic TypeScript syntax, can run tsc from a terminal, and understand JSON configuration files. This chapter shows how to adopt strict checking without blocking all feature work at once.
"strict": true enables a group of compiler checks that catch unsafe assumptions. In an existing project, enabling every check immediately can produce hundreds of errors. A staged migration lets the team fix the errors in useful groups.
Start with a Baseline
Create a branch for the migration and run the compiler without emitting JavaScript:
npx tsc --noEmitRecord the current errors before changing the configuration. Then enable strict mode:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"noEmit": true
},
"include": ["src"]
}The noEmit option tells TypeScript to check the project without writing output. This is useful during migration because you can measure progress without changing the build directory.
Fix Errors in Groups
Fix errors that represent missing information first. For example, noImplicitAny rejects parameters whose types TypeScript cannot infer:
function formatPrice(price) {
return `$${price.toFixed(2)}`;
}
function formatPriceTyped(price: number): string {
return `$${price.toFixed(2)}`;
}Next, fix nullability errors. With strictNullChecks, null and undefined are separate types. Code must handle them before using a value:
function getDisplayName(name: string | null): string {
return name === null ? "Anonymous" : name;
}Then address class initialization, unsafe function calls, and caught errors. A useful migration order is:
- Add missing parameter and return types.
- Handle
nullandundefinedat input boundaries. - Initialize required class properties.
- Narrow caught errors before reading their properties.
- Review function assignments and uses of
this. - Add tests for behavior changed by the fixes.
Do not solve every error by adding any or non-null assertions such as value!. Those choices silence the checker while preserving the original risk. Use them only at a documented boundary where you cannot improve the type immediately.
| Approach | Benefit | Cost |
|---|---|---|
| Enable all checks at once | Short configuration change | Large, disruptive error list |
| Disable one flag temporarily | Smaller migration steps | The disabled risk remains |
| Migrate package by package | Clear ownership and progress | Requires package boundaries |
Temporarily Disable One Check
You can keep the strict umbrella enabled while temporarily excluding one flag:
{
"compilerOptions": {
"strict": true,
"noImplicitAny": false
}
}Track the exception as migration work. Re-enable it when the related errors are fixed. A project should not leave exceptions undocumented, because new code can continue to depend on the weaker behavior.
Keep the Migration Enforced
Add npx tsc --noEmit to continuous integration (CI), the automated checks that run for each change. Developers can then work in small batches while every change remains type-checked.
Exercise
Why might a team set "strict": true and "noImplicitAny": false temporarily?
Show answer
This keeps the other strict checks active while the team adds missing types and removes implicit any errors in a later step.