object-transforms
NonNullable<T>
Removes null and undefined from the type T. In TS 4.9+ it is implemented as `T & {}`, which drops nullish members without altering the rest.
type NonNullable<T> = T & {} Usage patterns
| Pattern | Purpose |
|---|---|
| NonNullable<T> | Strip null and undefined from a union |
| NonNullable<T[K]> | Get a non-nullable value type from a property |
| arr.filter((x): x is NonNullable<T> => x != null) | Runtime filter paired with the type |
| Exclude<T, null | undefined> | Equivalent formulation using Exclude |
Examples
type A = string | number | null | undefined;
type B = NonNullable<A>; Result: string | number
function assert<T>(v: T): NonNullable<T> {
if (v == null) throw new Error('nullish');
return v as NonNullable<T>;
} Common non-null assertion helper
type Name = NonNullable<User['nickname']>; Pull the non-nullish variant of an optional property's value
type X = NonNullable<null>; Result: never — no non-null members remain
Gotcha
Only removes null and undefined — it does not remove `void`, `0`, `''`, or `false`. Also it doesn't strip the optional `?` modifier from a property; use Required for that.
Related utility types
Exclude<T, U>
Filters a union T by removing every member that is assignable to U. Distributes over union members via the conditional type, producing the set-difference T \ U.
Required<T>
Removes optionality from every property of T, producing a type where all fields must be present. The -? mapping modifier strips the ? flag from every key.
Extract<T, U>
Keeps only those members of union T that are assignable to U. The dual of Exclude, giving the set-intersection T ∩ U.
Awaited<T>
Models the type produced by awaiting T, recursively unwrapping nested Promises (and other thenables). Introduced in TS 4.5 and used internally by the `await` expression's type.
Partial<T>
Constructs a type where every property of T is set to optional. Useful for representing update payloads, patch objects, and configuration overrides where all fields may be omitted.
← All TS utility types · TypeScript vs JavaScript · any vs unknown vs never