A union type is a composite type formed by combining two or more constituent types using the pipe operator (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
|). It declares that a value conforms to at least one of the specified types at compile time. Because TypeScript employs structural typing, a value may satisfy multiple constituent types simultaneously. Union types are strictly a compile-time construct and are erased during JavaScript emission, having no effect on runtime evaluation.
Property Access and Intersection
When a value is typed as a union, TypeScript restricts direct property access to the intersection of the constituents’ properties. You can only invoke methods or access properties that are guaranteed to exist across all types within the union.Type Narrowing
To access members specific to a single constituent type, the union must be narrowed. TypeScript utilizes control flow analysis in conjunction with type guards (such astypeof, instanceof, the in operator, or custom type predicates) to deduce the specific type within a given lexical scope.
Discriminated Unions and Exhaustiveness Checking
A discriminated union (often called a tagged union) is a structural pattern where every constituent type in the union shares a common literal property, known as the discriminant. TypeScript leverages this discriminant to perform deterministic type narrowing, typically withinswitch statements or if/else chains.
A primary mechanical benefit of discriminated unions is exhaustiveness checking. By assigning the unhandled cases to the never type in a default block, the compiler will enforce that all possible constituent types are evaluated. If a new type is later added to the union without updating the control flow, TypeScript will raise a compile-time error.
Master TypeScript with Deep Grasping Methodology!Learn More





