Sub topic 7
Exclude, Extract and NonNullable
This chapter assumes that you know TypeScript unions, string literal types, interfaces, and generic type syntax. The utility types in this chapter filter existing types. They work at compile time, so they do not change JavaScript values at runtime.
Filtering union members
A union type represents one of several possible types:
type Status = "idle" | "loading" | "error" | "done";Exclude<T, U> removes from T every member that is assignable to U.
type ActiveStatus = Exclude<Status, "idle">;
// "loading" | "error" | "done"
type NotFinished = Exclude<Status, "done" | "error">;
// "idle" | "loading"The first type removes "idle". The second removes both "done" and "error".
Extract<T, U> does the opposite. It keeps only members of T that are assignable to U.
type FinishedStatus = Extract<Status, "done" | "error">;
// "error" | "done"
type LoadingStatus = Extract<Status, "loading" | "idle">;
// "loading"The order of the type arguments matters. Exclude<Status, "idle"> means “remove idle from Status.” Extract<Status, "idle"> means “keep members of Status that match idle.”
Removing null and undefined
NonNullable<T> removes null and undefined from a type.
type MaybeName = string | null | undefined;
type Name = NonNullable<MaybeName>;
// stringThis is useful after a value has been checked:
function displayName(name: string | null | undefined): string {
const safeName: NonNullable<typeof name> = name ?? "Anonymous";
return safeName.toUpperCase();
}The nullish coalescing operator, ??, supplies a fallback when name is null or undefined. The resulting value has type string, which matches NonNullable<typeof name>.
NonNullable does not validate data at runtime. If an untyped API sends an unexpected value, TypeScript cannot detect that value automatically.
Filtering object unions
These utilities also work with unions of object types. A discriminant is a property, such as kind, whose literal value identifies a union member.
type Event =
| { kind: "click"; x: number; y: number }
| { kind: "keypress"; key: string }
| { kind: "resize"; width: number; height: number };
type InputEvent = Extract<Event, { kind: "click" | "keypress" }>;
// { kind: "click"; x: number; y: number }
// | { kind: "keypress"; key: string }
type NonInputEvent = Exclude<Event, { kind: "click" | "keypress" }>;
// { kind: "resize"; width: number; height: number }The matching is structural. Extract keeps each object type that can be assigned to { kind: "click" | "keypress" }.
Combining filters
You can compose these utilities when a type has several constraints:
type ApiResult =
| { state: "ok"; data: string }
| { state: "error"; message: string }
| null
| undefined;
type PresentResult = NonNullable<ApiResult>;
// { state: "ok"; data: string }
// | { state: "error"; message: string }
type ErrorResult = Extract<PresentResult, { state: "error" }>;
// { state: "error"; message: string }First, NonNullable removes the absent cases. Then Extract selects the error object.
Common errors
ExcludeandExtractare compile-time operations. They do not remove or select values in an array.- A broad type can absorb a literal.
Extract<string, "admin">isnever, becausestringis not assignable to the narrower"admin". - Object filtering works best when every union member has a clear discriminant.
NonNullable<T>only removesnullandundefined; it does not remove empty strings, zero, or invalid objects.
Exercise
Given type Role = "admin" | "editor" | "viewer" | null, write a type that contains only the non-admin roles and no null value.
Show answer
type NonAdminRole = Exclude<NonNullable<Role>, "admin">;