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

NonNullable<T> Basics

Removing null and undefined from a type with NonNullable.

NonNullable<T> Basics

What you'll learn: How to use NonNullable<T> to strip away null and undefined from a type, ensuring you only work with actual values.

What NonNullable<T> Does

NonNullable<T> is a built-in utility type that removes null and undefined from a type. Think of it as a filter that only lets "real values" through — it says "I don't want nullable types anymore."

This is especially useful when you have a type that might be nullable, but you need to work with a version that guarantees a value exists.

Basic Syntax

type MaybeString = string | null | undefined;

type DefinitelyString = NonNullable<MaybeString>;
// Result: string

Here, MaybeString could be a string, null, or undefined. NonNullable<MaybeString> removes the null and undefined, leaving only string.

When It's Useful

After filtering or validation, you often know a value can't be null anymore:

type UserRole = "admin" | "user" | null;

function processRole(role: NonNullable<UserRole>) {
  // role is guaranteed to be "admin" | "user"
  // No null to worry about here!
}

This pairs well with array filtering or after type guards have eliminated nullish values.

What Stays Untouched

NonNullable<T> only removes null and undefined. Everything else remains:

type Mixed = string | number | null | boolean | undefined;

type Cleaned = NonNullable<Mixed>;
// Result: string | number | boolean

Key Takeaway: NonNullable<T> is your tool for creating a "definitely has a value" version of a type by removing null and undefined from unions.