Conditional types

T extends U ? A : B

A conditional type picks one of two types by testing another type. Read T extends U ? A : B as if T fits in U then A else B.

IsString answers yes for string and no for number. The test runs on types only and vanishes from the compiled code.

type IsString<T> = T extends string ? "yes" : "no"; const a: IsString<string> = "yes"; const b: IsString<number> = "no"; console.log(a, b);

yes no

Distributivity

When the tested type is a naked parameter like T, the condition distributes over unions. Each member is tested alone and the answers are joined back with a union.

ToArray sees string and number separately, so C becomes a string array or a number array. Wrap T in a tuple to stop this when you need the whole union at once.

type ToArray<T> = T extends unknown ? T[] : never; type C = ToArray<string | number>; const c1: C = ["a"]; const c2: C = [1]; console.log(typeof c1[0], typeof c2[0]);

string number

Filter with never

never is the type with no values, so union members mapped to never simply disappear. That makes never a handy filter.

NoStrings drops string and keeps the rest, leaving number or boolean. Exclude in the next section is built on exactly this trick.

type NoStrings<T> = T extends string ? never : T; type D = NoStrings<string | number | boolean>; const d: D = true; console.log(typeof d);

boolean

Read API types

Real code reads API unions with Exclude and Extract instead of hand-written conditions. Exclude drops the listed members while Extract keeps only the listed ones.

NotOk holds every status except ok, and OkOrLoading holds the two retryable ones. Both helpers are conditional types under the hood.

type Status = "ok" | "error" | "loading"; type NotOk = Exclude<Status, "ok">; type OkOrLoading = Extract<Status, "ok" | "loading">; const notOk: NotOk = "error"; const retry: OkOrLoading = "loading"; console.log(notOk, retry);

error loading

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How is T extends U ? A : B read?

    Answer

    If T fits in U then A, otherwise B

    The test asks whether T is assignable to U, and the answer selects one of the two types.

  2. When does a conditional type distribute over a union?

    Answer

    When the tested type is a naked type parameter

    A naked parameter is tested member by member, and the results are joined back into a union.

  3. How is distribution stopped when the whole union must be tested at once?

    Answer

    By wrapping it in a tuple, as [T] extends [U] ? A : B

    A tuple is not a naked parameter, so the test sees the union as one type.

  4. What happens to a union member that a conditional type maps to never?

    Answer

    It disappears from the union

    never has no values, so a union with it drops that member, which is how filters such as Exclude work.

  5. What does this program print? type IsString<T> = T extends string ? "yes" : "no"; const a: IsString<string> = "yes"; const b: IsString<number> = "no"; console.log(a, b);

    Answer

    yes no

    The conditional type resolves to the literal "yes" for string and "no" for number.

  6. What does this program print? type ToArray<T> = T extends unknown ? T[] : never; type C = ToArray<string | number>; const c1: C = ["a"]; const c2: C = [1]; console.log(typeof c1[0], typeof c2[0]);

    Answer

    string number

    Distribution gives string[] or number[], so the first array holds a string and the second a number.

  7. What does this program print? type NoStrings<T> = T extends string ? never : T; type D = NoStrings<string | number | boolean>; const d: D = true; console.log(typeof d);

    Answer

    boolean

    The string member is filtered out, so D is number or boolean and true fits.

  8. What does this program print? type Status = "ok" | "error" | "loading"; type NotOk = Exclude<Status, "ok">; type OkOrLoading = Extract<Status, "ok" | "loading">; const notOk: NotOk = "error"; const retry: OkOrLoading = "loading"; console.log(notOk, retry);

    Answer

    error loading

    Exclude drops ok, Extract keeps ok and loading, and the two constants hold error and loading.

  9. What does the compiler say about this program? type Status = "ok" | "error" | "loading"; type NotOk = Exclude<Status, "ok">; const bad: NotOk = "ok"; console.log(bad);

    Answer

    error TS2322: Type '"ok"' is not assignable to type 'NotOk'.

    Exclude removed ok from the union, so the literal no longer fits the alias.

  10. What does the compiler say about this program? type IsString<T> = T extends string ? "yes" : "no"; const x = IsString<string>; console.log(x);

    Answer

    error TS2693: 'IsString' only refers to a type, but is being used as a value here.

    A conditional type exists only at compile time, so it cannot be read as a value.

Article author: Arthur Isaev