NonNullable<T>

Constructs a type by excluding null and undefined from T.

Since TS 2.8 Spec ↗

Syntax

NonNullable<Type>

Returns

type — T with the null and undefined members removed.

Examples

type T = string | null | undefined;
type Clean = NonNullable<T>; // string
function first<T>(arr: T[]): NonNullable<T> {
  return arr[0] as NonNullable<T>;
}

Notes

Since TS 4.8 it is implemented as `T & {}`, which strips `null` and `undefined` while preserving the rest of the type. Useful after filtering nullish values or when narrowing optional results.

See also