Course contentsShow
← TypeScript
Lesson 693 of 1,79616. Utility TypesFree lesson

NonNullable in Strictness Contexts

How NonNullable interacts with strictNullChecks and optional chaining.

NonNullable in Strictness Contexts

What you'll learn: How NonNullable<T> behaves differently depending on your compiler's strictness settings, especially strictNullChecks.

The Core Idea

NonNullable<T> removes null and undefined from a type—but its practical impact depends entirely on whether you've enabled strictNullChecks in your tsconfig.json. Without strictness, TypeScript already treats null and undefined as assignable to everything, so NonNullable<T> has little effect. With strictness enabled, it becomes a powerful tool for guaranteeing non-nullable values.

Think of it like a water filter: if your water supply (your type system) already mixes clean and dirty water freely (no strictNullChecks), the filter doesn't help much. But when the supply keeps them separate (strictNullChecks: true), the filter does exactly what you need.

strictNullChecks Disabled

Without strictNullChecks, null and undefined are subtypes of everything. NonNullable<string> still produces string, but you could assign null to a string anyway, so the utility barely matters.

strictNullChecks Enabled

With strictNullChecks: true, null and undefined are distinct types. Now NonNullable<string | null> produces string, and you cannot assign null to that result without a compiler error. This is where NonNullable<T> shines—especially with optional chaining results or union types from APIs.

Practical synergy: When you use optional chaining (obj?.prop), you get Type | undefined. Wrapping that in NonNullable<...> explicitly strips the undefined, signaling to TypeScript (and your team) that you've handled the null case.

Key Takeaway: NonNullable<T> is most valuable in strict mode—without strictNullChecks, it offers little protection. Always enable strict typing to unlock its full safety benefits.