

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: Unions
incomplete
2: Optional Parameters
incomplete
3: Default Parameters
incomplete
4: Literal Types
incomplete
5: Value Unions
incomplete
6: Template Literal Types
incomplete
7: Giant Unions
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Take another look at our last example of a literal type:
function move(direction: "north") {
// Implementation...
}To make it a bit more useful, let's combine that idea with a union type:
function move(direction: "north" | "south" | "east" | "west") {
// Implementation...
}And then let's refactor it to make a new "Direction" type that we can reuse:
type Direction = "north" | "south" | "east" | "west";
function move(direction: Direction) {
// Implementation...
}At Support.ai, we're building a ticket prioritization system that needs a consistent way to categorize tickets by importance.
low
medium
high
critical
low returns 0
medium returns 1
high returns 2
critical returns 3
By default return 0
The default case is needed so that the function's inferred return type doesn't include undefined.