

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
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
Many other statically typed languages (including Go) don't have nearly as extensive and powerful type systems as TypeScript. It should be obvious because it's in the name, but TypeScript truly has a massive type system.
Literal types are incredibly powerful for narrowing the possible values of a variable.
So what if we want to declare a "direction" variable?
function move(direction: string) {
// Implementation...
}
This kinda sucks... direction can be any string! To be fair, in many languages enums are used to solve this problem. And while TypeScript does have enums, which we'll cover later, literal types are a more lightweight solution. A literal value can be used as a type:
function move(direction: "north") {
// Implementation...
}
Now direction can only be "north"!