strictNullChecks
strictNullChecks is the compiler flag that removes null and undefined from every other type. Absence becomes a union you have to handle before you read a property.
Detailed Explanation
Without this flag, TypeScript's model of JavaScript is the one that causes production incidents: every type includes null and undefined. A variable typed string may legally hold undefined, and user.name.toUpperCase() type-checks. The crash happens later.
With the flag on, string means a string. A value that might be missing is string | undefined or string | null. Reading .toUpperCase() on that union is an error until you narrow: if (name !== undefined), name?.toUpperCase(), or a guard. The compiler is forcing you to write the check you needed anyway.
strictNullChecks is a member of the strict family. Turning on strict turns this on. It is the flag that produces the most errors when you enable it on an existing codebase, because optional values are everywhere: array index reads, map lookups, DOM queries, function parameters with defaults, uninitialized fields.
What it changes, concretely:
nullandundefinedare distinct types, not members ofstring,object, or your interfaces.- Optional properties (
name?: string) arestring | undefinedwhen read. - Optional parameters are
T | undefined. strictPropertyInitialization(also instrict) then requires class fields to be assigned, because an unassigned field isundefined.- Control-flow analysis tracks assignments: after
if (!x) return,xis narrowed in the rest of the function.
What it does not change: runtime behavior. The flag does not insert checks. document.getElementById still returns HTMLElement | null because the DOM API returns null. You still write the if.
noUncheckedIndexedAccess is the sibling people turn on next. Even with strictNullChecks, arr[0] is typed T, not T | undefined. The extra flag makes index reads honest. It is not part of strict.
Migrating a large project is incremental. Enable strict and set "strictNullChecks": false as an override, then turn it back on per package or per folder using extends. Fixing the errors is mostly adding | undefined to types that were already wrong, and adding narrows at use sites. The types that were already honest get quieter.
The cost is ceremony around values that are "obviously" present. The benefit is that the most common JavaScript exception — reading a property of undefined — becomes a compile error at the line that assumed too much.
Code Examples
1. The error this flag exists to produce.
// 1. The error this flag exists to produce.
function upper(name: string | undefined): string {
// return name.toUpperCase();
// Error: 'name' is possibly 'undefined'.
return name?.toUpperCase() ?? "";
}
console.log(upper("ada")); // ADA
console.log(upper(undefined)); // (empty string)2. tsconfig: on by itself, or via strict.
// 2. tsconfig: on by itself, or via strict.
{
"compilerOptions": {
"strict": true,
"strictNullChecks": true
}
}3. Optional properties, and why index reads still lie without the extra flag.
// 3. Optional properties, and why index reads still lie without the extra flag.
type User = { nickname?: string };
function label(user: User): string {
return user.nickname?.toUpperCase() ?? "none";
}
const names: string[] = [];
const first = names[0];
// first is string under strictNullChecks alone — the crash remains.
// With noUncheckedIndexedAccess, first is string | undefined.Common Gotchas
`?.` and `??` are the usual fix, not `!`
a non-null assertion compiles and then throws if you were wrong. Narrow or provide a default.
Enabling this on a large codebase is a project, not a one-line change
expect thousands of errors, mostly real. Do not disable the flag globally to silence one file; use a local override or fix the types.
`strictNullChecks` does not include `noUncheckedIndexedAccess`
empty-array index reads still type as `T` until you turn the extra flag on.
`null` and `undefined` stay distinct
a function that accepts `string | undefined` still rejects `null`. Convert at the boundary.