Conditional types | Typescript

TypeScript 2.8 introduces conditional types

T extends U ? X : Y

The type above means when T is assignable to U the type is X, otherwise the type is Y.

interface Animal {
kind: string;
}
interface Bird {
name: string;
}
interface Cat {
name: string;
}
interface Dog extends Animal {
name: string;
}
type AnimalTypeName<T> =
T extends Animal ? Cat :
T extends Animal ? Dog :
T extends Animal ? Bird :
Animal
type t0 = AnimalTypeName<Cat>; // Animal
type t1 = AnimalTypeName<Dog>; // Cat
type t2 = AnimalTypeName<Animal>; // Cat
type t3 = AnimalTypeName<Bird>; // Animal

Distributive conditional types#

interface A {}
interface B {}
interface C {}
interface D {}
interface X {}
interface Y {}
type TypeName = (A | B | C) extends D ? X : Y;

Then the last line is equivalent to:

(A extends D ? X : Y) | (B extends D ? X : Y) | (C extends D ? X : Y)

Diff example#

type Diff<T, U> = T extends U ? never : T;
// (string extends boolean ? never : string) | (number extends boolean ? never: number) | (boolean extends boolean ? never: boolean)
type TypeName = Diff<string| number | boolean, boolean>; // string | number

Filter example#

type Filter<T, U> = T extends U ? T : never;
// (string extends boolean ? string: never) | (number extends boolean ? number: never) | (boolean extends boolean ? boolean: never)
type TypeName = Filter<string| number | boolean, boolean>; // boolean

Type inference in conditional types#

Within the extends clause of a conditional type, it is now possible to have infer declarations that introduce a type variable to be inferred.

type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any;

Conditional types can be nested to form a sequence of pattern matches that are evaluated in order:

type Unpacked<T> =
T extends (infer U)[] ? U :
T extends (...args: any[]) => infer U ? U :
T extends Promise<infer U> ? U :
T;
type T0 = Unpacked<string>; // string
type T1 = Unpacked<string[]>; // string
type T2 = Unpacked<() => string>; // string
type T3 = Unpacked<Promise<string>>; // string
type T4 = Unpacked<Promise<string>[]>; // Promise<string>
type T5 = Unpacked<Unpacked<Promise<string>[]>>; // string

Predefined conditional types#

TypeScript 2.8 adds several predefined conditional types to lib.d.ts:

  • Exclude<T, U> -- Exclude from T those types that are assignable to U.
  • Extract<T, U> -- Extract from T those types that are assignable to U.
  • NonNullable<T> -- Exclude null and undefined from T.
  • ReturnType<T> -- Obtain the return type of a function type.
  • InstanceType<T> -- Obtain the instance type of a constructor function type.

Example#

type T00 = Exclude<"a" | "b" | "c" | "d", "a" | "c" | "f">; // "b" | "d"
type T01 = Extract<"a" | "b" | "c" | "d", "a" | "c" | "f">; // "a" | "c"
type T02 = Exclude<string | number | (() => void), Function>; // string | number
type T03 = Extract<string | number | (() => void), Function>; // () => void
type T04 = NonNullable<string | number | undefined>; // string | number
type T05 = NonNullable<(() => string) | string[] | null | undefined>; // (() => string) | string[]
function f1(s: string) {
return { a: 1, b: s };
}
class C {
x = 0;
y = 0;
}
type T10 = ReturnType<() => string>; // string
type T11 = ReturnType<(s: string) => void>; // void
type T12 = ReturnType<(<T>() => T)>; // {}
type T13 = ReturnType<(<T extends U, U extends number[]>() => T)>; // number[]
type T14 = ReturnType<typeof f1>; // { a: number, b: string }
type T15 = ReturnType<any>; // any
type T16 = ReturnType<never>; // never
type T17 = ReturnType<string>; // Error
type T18 = ReturnType<Function>; // Error
type T20 = InstanceType<typeof C>; // C
type T21 = InstanceType<any>; // any
type T22 = InstanceType<never>; // never
type T23 = InstanceType<string>; // Error
type T24 = InstanceType<Function>; // Error

Note: The Exclude type is a proper implementation of the Diff type suggested here. We've used the name Exclude to avoid breaking existing code that defines a Diff, plus we feel that name better conveys the semantics of the type.