We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Value Unions

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...
}

Assignment

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.