Sub topic 15
Turn on strict mode step by step
Assume your project already compiles JavaScript and TypeScript together, and you can run npx tsc from its root directory. Strict mode is a group of compiler checks. It reports more possible mistakes at compile time, but it does not change how JavaScript runs.
Turn on strict checking after the code has a working baseline. Enable one related check at a time, fix its errors, and run the tests after each group. This keeps the migration reviewable.
Start with a clean baseline
Run the compiler without emitting files:
npx tsc --noEmitThe --noEmit option checks the project but does not create JavaScript output. Save this result before changing the configuration. If it already reports errors, fix or record those errors first so new errors are easy to identify.
Enable checks in stages
A tsconfig.json can begin with individual checks disabled or enabled:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"rootDir": "src",
"outDir": "dist",
"allowJs": true,
"checkJs": false,
"esModuleInterop": true,
"skipLibCheck": true,
"noImplicitAny": false,
"strictNullChecks": false
},
"include": ["src"]
}Enable the checks in an order that matches the errors in your project:
noImplicitAnyrejects parameters and variables whose types cannot be inferred. Add annotations or improve the types of surrounding values.strictNullCheckstreatsnullandundefinedas distinct types. Check a value before using a property or method that might be absent.strictFunctionTypeschecks that function parameters are compatible when functions are assigned or passed as values.useUnknownInCatchVariablestypes caught errors asunknown, so error values must be checked before reading their properties.noImplicitThisreports functions whosethisvalue has no known type.
After these checks pass, replace the individual settings with the strict group:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"rootDir": "src",
"outDir": "dist",
"allowJs": true,
"esModuleInterop": true,
"skipLibCheck": true,
"strict": true
},
"include": ["src"]
}strict: true enables the main strict-family checks, including noImplicitAny, strictNullChecks, and strictFunctionTypes. Keep explicit settings when you need a documented project exception. For example, a temporary allowJs setting can remain while JavaScript files are still being migrated.
Verify the migration continuously
The compiler is part of the project workflow, not a one-time migration tool. Build the project with npx tsc, run the tests, and add npx tsc --noEmit to continuous integration. A pull request should fail when new type errors appear.
Exercise
Your project has many missing parameter types but few nullable values. Which strict check should you enable first, and why?
Show answer
Enable noImplicitAny first. It targets missing inferred types, so its errors can be fixed without mixing them with strictNullChecks errors about null and undefined.