Union type

A value that may be one of several types, written with the | separator.

Since TS 1.4 Spec ↗

Syntax

A | B | C

Examples

type Id = string | number;

function format(id: Id) {
  return typeof id === 'string' ? id.trim() : id.toFixed();
}
let status: 'idle' | 'loading' | 'done' = 'idle';

Notes

A union accepts any of its member types. Before using a member-specific property you must narrow the union with `typeof`, `instanceof`, `in`, or a type guard. Only members common to all parts are accessible without narrowing.

See also