Skip to main content

Enums

September 14, 2024Less than 1 minute

Enums

  • TypeScript also offers enum. For example, a race in our app can be either ready, started or done.
enum RaceStatus {
    Ready,
    Started,
    Done
}
const race = new Race();
race.status = RaceStatus.Ready;
  • The enum is in fact a numeric value, starting at 0. You can set the value you want, though:
enum Medal {
    Gold = 1,
    Silver,
    Bronze
}
  • You can also specify a string value:
enum Position {
    First = 'First',
    Second = 'Second',
    Other = 'Other'
}