TypeScript Strict Mode: Best Practices
2026-03-10
Enabling strict mode in TypeScript catches real bugs at compile time. Learn the most impactful options and patterns for writing safer, more maintainable code.
Why Strict Mode?
TypeScript's strict flag is a shorthand that enables a collection of checks that prevent common runtime errors. Projects that skip it often accumulate subtle null-reference and type-widening bugs.
Add it to tsconfig.json:
{
"compilerOptions": {
"strict": true
}
}
What Gets Enabled
strict turns on:
strictNullChecks—nullandundefinedare not assignable to other typesnoImplicitAny— variables without an inferred type must be explicitly typedstrictFunctionTypes— stricter checking on function parameter typesstrictPropertyInitialization— class properties must be initialized in the constructor
Patterns That Help
Prefer unknown over any
function parseJson(input: string): unknown {
return JSON.parse(input)
}
Force callers to narrow the type before use.
Use Discriminated Unions
type Result<T> =
| { status: 'ok'; data: T }
| { status: 'error'; message: string }
Exhaustive switch statements on status will catch missing cases at compile time.
Type-Only Imports
import type { User } from './types'
Prevents accidental runtime imports of type-only modules and makes bundler tree-shaking more reliable.
Incremental Adoption
For existing codebases, enable options one at a time, starting with strictNullChecks. Fix the errors in each batch before moving to the next flag.
Strict mode TypeScript is one of the highest-value investments in a codebase's long-term health.