TypeScript Tutorial #6: Union Types, Literal Types, and Type Aliases
In the previous tutorial, we learned about objects and interfaces. Now let’s learn about union types, literal types, and type aliases — patterns that make TypeScript truly powerful. By the end of this tutorial, you will know how to use union types, literal types, discriminated unions, intersection types, and type aliases. Union Types A union type means “this value can be one of several types.” Use the | (pipe) symbol: let id: string | number; id = "abc-123"; // OK — string id = 42; // OK — number id = true; // Error: Type 'boolean' is not assignable to type 'string | number' Union types are everywhere in real code. A function that accepts multiple types: ...