Enums provide enumerations for grouping together constants.
In TypeScript, enums can automatically “scale” / assign values to objects, part of the enum:
Alternatives
Enums are pretty much disabled in TypeScript by default. The reason is the Erasable Syntax Only flag, which is set to false by default. Erasable meaning disappearing at runtime. As enums create objects at runtime, they belong into this group.
Using as consts:
export const Status = { IDLE: 'idle', LOADING: 'loading', SUCCESS: 'success', ERROR: 'error', } as const;
// Then, exporting for type-usage:
export type StatusType = keyof typeof Status;
Resources
- When to use enums and when not: https://bluepnume.medium.com/nine-terrible-ways-to-use-typescript-enums-and-one-good-way-f9c7ec68bf15