Course contentsShow
← TypeScript
Lesson 1182 of 1,79628. tsconfig in DepthFree lesson

strictNullChecks Fundamentals

Why strictNullChecks separates null and undefined from all other types, preventing common runtime errors.

strictNullChecks Fundamentals

You'll learn why strictNullChecks is one of TypeScript's most important safety features, preventing an entire category of runtime crashes by treating null and undefined as distinct types.

The Billion-Dollar Mistake

In JavaScript (and many languages), variables can be null or undefined almost anywhere. This creates a notorious problem: accessing properties or calling methods on null/undefined values crashes your program. Tony Hoare, who invented null references, called it his "billion-dollar mistake."

Without strictNullChecks, TypeScript mirrors JavaScript's permissive behavior. A variable typed as string can secretly be null or undefined, and TypeScript won't warn you until it crashes at runtime.

What strictNullChecks Changes

When strictNullChecks is enabled in your tsconfig.json, TypeScript fundamentally changes how it treats null and undefined:

Before strictNullChecks: null and undefined are assignable to every type. A string can be null, a number can be undefined, and TypeScript says nothing.

After strictNullChecks: null and undefined become their own separate types. If you want a value to possibly be null or undefined, you must explicitly say so with a union type like string | null or number | undefined.

The Safety Benefit

This separation forces you to handle the "no value" case explicitly. You can't accidentally call .toUpperCase() on something that might be null. TypeScript will require you to check first — exactly like you learned with narrowing and type guards — turning runtime crashes into compile-time errors.

Key Takeaway: strictNullChecks separates null and undefined from all other types, making them opt-in rather than implicit, and eliminating one of JavaScript's most common sources of runtime errors.