What Is TypeScript Strict Mode?
TypeScript's strict flag is a shorthand that enables a group of type-checking options that together catch a much wider class of bugs. When you turn on "strict": true in your tsconfig.json, TypeScript enables:
- strictNullChecks — null and undefined are not assignable to other types
- noImplicitAny — variables without a type annotation can't have implicit
any - strictFunctionTypes — function parameter types are checked contravariantly
- strictBindCallApply — bind/call/apply are type-checked against their target function
- strictPropertyInitialization — class properties must be initialized in the constructor
- noImplicitThis —
thisexpressions without an explicit type raise an error - useUnknownInCatchVariables — caught errors in catch blocks are typed as
unknown
Why You Should Enable Strict Mode
Strict mode is where TypeScript's real value lives. Without it, you can write TypeScript that doesn't actually prevent the most common runtime errors. With strict mode, TypeScript catches null pointer dereferences, implicit any types that bypass type checking, and incorrect function signatures — before your code runs.
The TypeScript team recommends starting every new project with strict mode. For existing projects, the incremental approach below makes it manageable.
How to Enable Strict Mode Incrementally
For an existing codebase, enabling all strict options at once will produce hundreds of errors. Instead, enable them one at a time in order of impact:
// tsconfig.json
{
"compilerOptions": {
"strictNullChecks": true
}
}Fix all errors from strictNullChecks first, then add noImplicitAny, then the rest. This keeps the error count manageable.
Fixing strictNullChecks Errors
The most common strict mode errors come from strictNullChecks. The pattern is: Object is possibly null or undefined.
Pattern 1: Add a null check
// Before
const name = user.name.toUpperCase(); // error if user.name is null
// After
const name = user.name?.toUpperCase() ?? "";Pattern 2: Use a type assertion when you know the value is non-null
const element = document.getElementById("app")!; // non-null assertionPattern 3: Narrow the type with a guard
if (user.name !== null) {
const name = user.name.toUpperCase(); // TypeScript knows it's non-null here
}Fixing noImplicitAny Errors
The error Parameter 'x' implicitly has an 'any' type appears when TypeScript can't infer a type. Fix it by adding explicit annotations:
// Before
function process(data) { ... }
// After
function process(data: UserData): ProcessedResult { ... }Fixing strictPropertyInitialization Errors
Class properties must be initialized. Three options:
class Service {
// Option 1: initialize inline
private cache: Map<string, string> = new Map();
// Option 2: initialize in constructor
private db: Database;
constructor(db: Database) {
this.db = db;
}
// Option 3: declare as possibly undefined
private config?: Config;
}Conclusion
TypeScript strict mode is non-negotiable for production-quality code. Enable it incrementally, starting with strictNullChecks, and work through each category of errors. The bugs you catch during this process are bugs that would have reached production otherwise. For a full reference of TypeScript error codes, see our TypeScript Errors guide.
