Unions allow variables to hold one of several specified types.
type ColorValue = number | "red" | "green" | "blue";
type WindowState = "open" | "closed" | "minimized";
function wrapInArray(obj: string | string[]): string[] {
// If obj is a string, wrap it in an array; else, return the array as-is.
return typeof obj === "string" ? [obj] : obj;
}
// Example usage:
console.log(wrapInArray("hello")); // Output: ["hello"]
console.log(wrapInArray(["hello", "hi"])); // Output: ["hello", "hi"]
