Lesson 5 of 56 · Typescript
Literal Types & Enums
Duration: 20 minutes
Literal Types
A literal type is the exact value of a primitive.
type YesNo = "yes" | "no";
let answer: YesNo = "yes"; // OK
Numeric Literal Types
type OneToThree = 1 | 2 | 3;
let n: OneToThree = 2;
Enums
Enumerations provide a named set of values.
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT"
}
let dir: Direction = Direction.Up;
Const enums (inlined at compile time)
const enum Color { Red, Green, Blue }
let c = Color.Green; // emitted as 1