Union types allow a value to be one of several types:
let id: string | number;
id = "abc"; // OK
id = 123; // OK
id = true; // Error
function format(value: string | number) {
if (typeof value === "string") {
return value.toUpperCase();
}
return value.toFixed(2);
}