Union and Intersection Types
Union and intersection types in TypeScript allow you to write more flexible and expressive type definitions. A union type uses the pipe symbol (|) to indicate that a value can be one of several types.
An intersection type uses the ampersand (&) to combine multiple types into one. The resulting type must satisfy all the properties of each type involved.
1234567891011121314// Union type: a value can be a string OR a number let userId: string | number; userId = "abc123"; userId = 42; // Intersection type: a value must have properties of both types type Name = { name: string }; type Age = { age: number }; type Person = Name & Age; const person: Person = { name: "Alice", age: 30, };
In the code above, userId can be either a string or a number, so you can assign both "abc123" and 42 to it. This is helpful when a variable might accept more than one type, such as an ID that could be stored as a string or a number.
In the code sample, the Person type is an intersection of the Name and Age types, so an object of type Person must have both a name property of type string and an age property of type number. This is useful when you want to combine several type definitions into a single, more specific one.
Choosing between union and intersection types depends on your needs: use a union when a value can be one of several types, and use an intersection when a value must satisfy all the types at once.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Can you give more examples of union and intersection types?
When should I use a union type versus an intersection type?
Are there any common pitfalls when using these types in TypeScript?
Awesome!
Completion rate improved to 8.33
Union and Intersection Types
Swipe um das Menü anzuzeigen
Union and intersection types in TypeScript allow you to write more flexible and expressive type definitions. A union type uses the pipe symbol (|) to indicate that a value can be one of several types.
An intersection type uses the ampersand (&) to combine multiple types into one. The resulting type must satisfy all the properties of each type involved.
1234567891011121314// Union type: a value can be a string OR a number let userId: string | number; userId = "abc123"; userId = 42; // Intersection type: a value must have properties of both types type Name = { name: string }; type Age = { age: number }; type Person = Name & Age; const person: Person = { name: "Alice", age: 30, };
In the code above, userId can be either a string or a number, so you can assign both "abc123" and 42 to it. This is helpful when a variable might accept more than one type, such as an ID that could be stored as a string or a number.
In the code sample, the Person type is an intersection of the Name and Age types, so an object of type Person must have both a name property of type string and an age property of type number. This is useful when you want to combine several type definitions into a single, more specific one.
Choosing between union and intersection types depends on your needs: use a union when a value can be one of several types, and use an intersection when a value must satisfy all the types at once.
Danke für Ihr Feedback!