Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Union and Intersection Types | Working with Complex and Composed Types
TypeScript Types Fundamentals

bookUnion 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, };
copy

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.

question mark

Which scenario is best for using a union type instead of an intersection type?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 3

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Awesome!

Completion rate improved to 8.33

bookUnion and Intersection Types

Glissez pour afficher le menu

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, };
copy

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.

question mark

Which scenario is best for using a union type instead of an intersection type?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 3
some-alt