Skip to content
webtype.orgwebtype.org

    ↑↓ move · ⏎ open · esc close

    Unions

    NonNullable

    Removes null and undefined from a type.

    What it is

    Worth reading the definition for. It used to be a distributive conditional; since TypeScript 4.8 it is T & {}, because intersecting with the empty object type removes exactly the two types that cannot have properties. Shorter, faster, and no longer distributive — which matters only in the corners.

    Examples

    • NonNullable<string | null>
      →string
    • NonNullable<string | undefined | null>
      →string
    • NonNullable<null>
      →never

    Each resolved type above was printed by TypeScript 6.0.3, not written by hand.

    What it does not do

    • It does not touch a nested property. NonNullable<{ a: string | null }> is unchanged — the nullability is inside, and this works on the outside.
    • It does not narrow a value at run time. A variable typed string | null still needs a check; this only describes the type after one.

    Takeaway

    The definition changed under everyone in 4.8 and almost nobody noticed, which is the argument for quoting definitions instead of remembering them.