Backbencher.dev

Change Enum Number Sequence in TypeScript

Last updated on 12 Jan, 2023

TypeScript has an enum type. The sequence of numbering starts from zero by default. We can change the number sequencing in TypeScript.

Default Sequence

Here is an enum type:

enum Role {
  ADMIN,
  READ_ONLY,
  AUTHOR,
}

Here ADMIN gets 0, READ_ONLY gets 1 and AUTHOR gets 2.

What if we want ADMIN to have 1, READ_ONLY to have 2 and AUTHOR to have 5?

We can do like below:

enum Role {
  ADMIN = 1,
  READ_ONLY,
  AUTHOR = 5,
}

In the above example READ_ONLY gets number 2 by auto increment from previous one.

--- ○ ---
Joby Joseph
Web Architect