Conditional Types

NonNullable<T>

Constructs a type by excluding null and undefined from T, ensuring the resulting type is always a definite value.

Definition

TypeScript
type NonNullable<T> = T & {};

Examples

Removing Nullability
type MaybeUser = string | null | undefined;

type User = NonNullable<MaybeUser>;
// string

function greet(name: User) {
  console.log(`Hello, ${name}`);
}

greet("Alice"); // OK
// greet(null); // Error
Safe Property Access
interface Config {
  apiKey?: string;
  timeout?: number;
}

function requireField<T, K extends keyof T>(
  obj: T,
  key: K,
): NonNullable<T[K]> {
  const value = obj[key];
  if (value == null) throw new Error(`${String(key)} is required`);
  return value as NonNullable<T[K]>;
}

const config: Config = { apiKey: "abc123" };
const key: string = requireField(config, "apiKey");
Filtering Arrays
const items: (string | null | undefined)[] = [
  "apple",
  null,
  "banana",
  undefined,
  "cherry",
];

const filtered: NonNullable<string | null | undefined>[] =
  items.filter((x): x is NonNullable<typeof x> => x != null);
// ["apple", "banana", "cherry"]

Common Use Cases

  • 1Stripping null and undefined after validation
  • 2Typing the result of required field checks
  • 3Filter predicates that narrow array elements
  • 4Ensuring function return types are definite
  • 5Building assertion functions for nullable inputs

Understanding NonNullable<T>

NonNullable<T> removes null and undefined from a type T, producing a type that is guaranteed to be a definite, non-nullish value. In modern TypeScript it is defined as T & {}, which leverages the intersection with an empty object type to filter out null and undefined.

This utility type is essential in codebases that use TypeScript's strict null checks (the strictNullChecks compiler option). With strict null checks enabled, types like string | null or number | undefined are common, and NonNullable provides a clean way to express "this value has been validated and is definitely not null or undefined."

One of the most practical uses is in array filtering. When you have an array of possibly-null values and filter out the nulls, TypeScript doesn't automatically narrow the array's element type. By using a type predicate like (x): x is NonNullable<typeof x> => x != null, you get a properly typed array of definite values.

NonNullable is also invaluable for generic utility functions. If you write a function that asserts a value is not null, the return type should be NonNullable<T> to reflect that guarantee. This lets downstream code work with the value without additional null checks.

The difference between NonNullable and Exclude<T, null | undefined> is mostly stylistic—they produce the same result. NonNullable is more readable and expresses the intent more clearly. It is a specialized version of Exclude tailored to the extremely common case of removing nullability.

Related Types

More Conditional Types

Explore TypeScript Types

Browse our complete reference of 30 TypeScript utility types with definitions, examples, and explanations.