// Practical example //https://www.typescriptlang.org/docs/handbook/advanced-types.html#union-types function padLeft(value: string, padding: string | number) { if (typeof padding === "number") { return Array(padding + 1).join(" ") + value; } if (typeof padding === "string") { return padding + value; } throw new Error(`Expected string or number, got '${padding}'.`); } console.log(padLeft('hello', 'world')); //'worldhello' console.log(padLeft('hello', 10)); // hello // console.log(padLeft('hello', true)); //Fails to compile