bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/TypeScript/TypeScript Core
TypeScript•TypeScript Core

TypeScript Enums

An enum is a special "class" that represents a group of constants (unchangeable variables).

Enums come in two flavors string and numeric .

Let's start with numeric.

Numeric Enums - Default

By default, enums will initialize the first value to 0 and add 1 to each additional value:

Example

enum CardinalDirections {
  North, East, South, West
}
let currentDirection = CardinalDirections.North;
// logs 0 console.log(currentDirection); // throws error as 'North' is not a valid enum currentDirection = 'North'; // Error: "North" is not assignable to type 'CardinalDirections'.

Numeric Enums - Initialized

You can set the value of the first numeric enum and have it auto increment from that:

Example

enum CardinalDirections {
  North = 1, East, South, West
}
// logs 1 console.log(CardinalDirections.North); // logs 4 console.log(CardinalDirections.West);

Numeric Enums - Fully Initialized

You can assign unique number values for each enum value.

Then the values will not be incremented automatically:

Example

enum StatusCodes {
  NotFound = 404, Success = 200, Accepted = 202, BadRequest = 400
}
// logs 404 console.log(StatusCodes.NotFound); // logs 200 console.log(StatusCodes.Success);

String Enums

Enums can also contain strings .

This is more common than numeric enums, because of their readability and intent.

Example

enum CardinalDirections {
  North = 'North', East = "East", South = "South", West = "West"
};
// logs "North" console.log(CardinalDirections.North); // logs "West" console.log(CardinalDirections.West);

Technically, you can mix and match string and numeric enum values, but it is recommended not to do so.

Previous

TypeScript Object Types

Next

TypeScript Type Aliases and Interfaces

This chapter

Overview
40

Lessons

241m

Read time

1. TypeScript Tutorial2. TypeScript Introduction3. TypeScript Getting Started4. TypeScript Simple Types5. TypeScript Explicit Types and Inference6. TypeScript Special Types7. TypeScript Arrays8. TypeScript Tuples9. TypeScript Object Types10. TypeScript Enums11. TypeScript Type Aliases and Interfaces12. TypeScript Union Types13. TypeScript Functions14. TypeScript Casting15. TypeScript Classes16. TypeScript Basic Generics17. TypeScript Utility Types18. TypeScript Keyof19. TypeScript Null & Undefined20. TypeScript Definitely Typed21. TypeScript 5.x Updates22. TypeScript Configuration23. TypeScript with Node.js24. TypeScript with React25. TypeScript Tooling26. TypeScript Advanced Types27. TypeScript Type Guards28. TypeScript Conditional Types29. TypeScript Mapped Types30. TypeScript Type Inference31. TypeScript Literal Types32. TypeScript Namespaces33. TypeScript Index Signatures34. TypeScript Declaration Merging35. TypeScript Async Programming36. TypeScript Decorators37. TypeScript in JavaScript Projects (JSDoc)38. TypeScript Migration Guide39. TypeScript Error Handling40. TypeScript Best Practices

On this page

Numeric Enums - DefaultNumeric Enums - InitializedNumeric Enums - Fully InitializedString Enums