

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Intersections of Types
incomplete
2: The Never Type
incomplete
3: Intersecting Incompatible Types
incomplete
4: Intersections vs. Unions
incomplete
5: Super Set Unions
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
So we've covered how unions (|) and intersections (&) are both used to smoosh types together... but which should you use?
Use the | operator (suspiciously similar to the logical OR operator)
Widen the resulting type (more possible values)
Useful for modeling mutually exclusive options or states
Use the & operator (like logical AND)
Narrow the resulting type (fewer possible values)
Useful for combining multiple constraints or adding more required properties to existing types
type Human = {
name: string;
age: number;
};
type Elf = {
name: string;
ears: "pointy";
};
// Must have name, age, and pointy ears
type ElfHuman = Human & Elf;
// Must have name (shared between Human and Elf)
// Can have either age or ears (or both)
type ElfOrHuman = Human | Elf;Use unions to say your type is "this OR that"
Use intersections to say your type is "this AND that", or sometimes more simply, "this with the additional properties of that"
Support.ai stores information about different types of users who interact with support tickets. However, there's a problem, our SupportAgentUser type is impossible to use (incompatible never).