union types

A type formed from two or more other types. A value of union type can be any one of those types.

Syntax

typescript
type UnionType = TypeA | TypeB | TypeC;

Example

typescript
type StringOrNumber = string | number;
type Status = "loading" | "success" | "error";

function formatId(id: string | number): string {
  return typeof id === "string" ? id : id.toString();
}

const status: Status = "loading";