Strict compiler options
TypeScript · Reference cheat sheet
Strict compiler options
TypeScript · Reference cheat sheet
📋 Overview
strict: true enables a suite of soundness checks. Turn it on for new projects; migrate incrementally with per-flag toggles. TS 5.x also adds related flags (exactOptionalPropertyTypes, noUncheckedIndexedAccess, verbatimModuleSyntax).
🔧 Core concepts
Flag (under strict) | Effect |
|---|---|
noImplicitAny | Error on implied any |
strictNullChecks | null / undefined not in every type |
strictFunctionTypes | Contravariant params for function types |
strictBindCallApply | Typed bind / call / apply |
strictPropertyInitialization | Class props must be set |
noImplicitThis | this must be typed |
alwaysStrict | Emit "use strict" |
useUnknownInCatchVariables | catch (e) → unknown |
Extra (recommended):
noUncheckedIndexedAccess—T[K]includesundefined.exactOptionalPropertyTypes— distinguish missing vsundefined.noImplicitReturns/noFallthroughCasesInSwitch.verbatimModuleSyntax— type-only imports must useimport type.
💡 Examples
// tsconfig.json
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"noImplicitOverride": true,
"useUnknownInCatchVariables": true,
"verbatimModuleSyntax": true
}
}// strictNullChecks
function len(s: string | null) {
// return s.length; // error
return s?.length ?? 0;
}
// strictPropertyInitialization
class User {
name!: string; // definite assignment assertion — use sparingly
constructor(name: string) {
this.name = name;
}
}
try {
/* ... */
} catch (e) {
if (e instanceof Error) console.error(e.message);
}
import type { UserId } from "./ids"; // verbatimModuleSyntax⚠️ Pitfalls
- Enabling
stricton a large JS codebase surfaces many errors — fix by folder / flag. noUncheckedIndexedAccessadds| undefinedeverywhere — handle explicitly.exactOptionalPropertyTypesbreaks\{ prop?: T \}assignability withundefined.skipLibCheck: truehides issues in.d.ts(common, but masks problems).- Don’t disable
strictto “save time” — prefer localas/ narrowing.