Strict Mode Options
In this page:
Enabling strict
The strict option enables a whole collection of stronger type-checking flags at once, which is why most style guides recommend turning it on from the very first commit rather than retrofitting it later.
Example: Enabling strict
// tsconfig.json
// { "compilerOptions": { "strict": true } }
console.log("strict enables a whole bundle of stronger checks at once");
strictNullChecks
strictNullChecks makes null and undefined distinct types instead of silently assignable to everything, forcing code to explicitly handle the possibility of a missing value before using it — this alone catches a huge class of runtime crashes.
Example: strictNullChecks
function getLength(s: string | null): number {
if (s === null) return 0;
return s.length; // safe: s narrowed to string here
}
console.log(getLength(null));
noImplicitAny
noImplicitAny reports an error anywhere TypeScript would otherwise silently infer the any type for an untyped value, closing one of the most common ways type safety quietly leaks out of a codebase.
Example: noImplicitAny
function double(x: number): number {
return x * 2;
}
// Without noImplicitAny, an untyped parameter would silently become 'any'.
console.log(double(5));
strictFunctionTypes and strictBindCallApply
Strict function-type and bind/call/apply checks improve safety when functions are assigned to variables or invoked indirectly, catching parameter-type mismatches that looser settings would let through.
Example: strictFunctionTypes and strictBindCallApply
function greet(name: string): void {
console.log("Hello", name);
}
const fn: (name: string) => void = greet;
fn("Ravi");
Choosing Individual Strict Options
Most projects benefit from just turning on strict: true, but individual sub-options can be enabled one at a time when migrating an older, loosely-typed codebase that can't absorb every strict check at once.
Example: Choosing Individual Strict Options
// tsconfig.json
// { "compilerOptions": { "strictNullChecks": true, "noImplicitAny": false } }
console.log("Individual flags can be enabled incrementally during migration");
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: