Literal Types
String Literals
let foo: 'Hello';let foo: 'Hello';
foo = 'Bar'; // Error: "Bar" is not assignable to type "Hello"type CardinalDirection =
| "North"
| "East"
| "South"
| "West";
function move(distance: number, direction: CardinalDirection) {
// ...
}
move(1,"North"); // Okay
move(1,"Nurth"); // Error!Other literal types
Inference
Use cases
String based enums
Modelling existing JavaScript APIs
Discriminated Unions
Last updated