Sometimes you want a function to accept more than one type but still return a consistent result. Instead of repeating string | number everywhere, you can alias the union type. You can use typeof to narrow to the correct branch and handle each case.
// Alias the union type
type StringOrNum = string | number;
function toLength(value: StringOrNum): number {
if (typeof value === "string") {
return value.length; // here it's type string
}
return value; // here it's type number
}