TypeScript Enums are a feature carrying a named set of constants. TypeScript supports numeric enums and string enums. You can provide a name and value, like a key-value pair, for your enums.
This tutorial shows you how to retrieve all available values of a TypeScript enum
.
TypeScript Series Overview
- How to Export Transpiled Code From package.json
- Use Nodemon to Restart Your Server on Changes
- How to Allow Trailing Commas (Comma-Dangle) With Typescript-ESLint
- Use SWC to Speed up TypeScript Code
- Fixing Null Type Ignored in Union Type
- How to Remove Index Signature from a Type
- Module Augmentation Overwrites Declarations Instead of Merging Them
- Get All Keys of an Enum
- Get All Values of an Enum
- Using a String as Enum Key
- Understanding “keyof typeof”
- Get Type of Class Properties and Omit Methods
Get All Values of a TypeScript Enum
Enums are similar to JavaScript objects. You’re providing a key and (an optional) value. And because an enum
is related to objects, you can use JavaScript’s Object.values()
method to retrieve all values.
Here’s an example of a Future Studio books enum that gets the list of all values using Object.values()
:
export enum FutureStudioBooks {
retrofit = 'Retrofit Book',
picasso = 'Picasso Book',
glide = 'Glide Book',
gson = 'Gson Book',
'gson-workbook' = 'Gson Workbook',
}
const values = Object.values(LegitimizationJobTitle)
// ['Retrofit Book', 'Picasso Book', 'Glide Book', 'Gson Book', 'Gson Workbook']
The values
property now contains all enum values in an array.
Narrowing the TypeScript Enum Values Type
The TypeScript return type definition of Object.values
is an array of the generic input type that is passed to the method. In this example, we’re getting an array of FutureStudioBooks[]
:
export enum FutureStudioBooks {
retrofit = 'Retrofit Book',
picasso = 'Picasso Book',
glide = 'Glide Book',
gson = 'Gson Book',
'gson-workbook' = 'Gson Workbook',
}
const values = Object.values(FutureStudioBooks)
// const values: FutureStudioBooks[]
Here’s TypeScript’s method signature of Object.values
:
interface ObjectConstructor {
values<T>(o: { [s: string]: T; } | ArrayLike<T>): T[];
}
You can see that TypeScript already derives the input type and uses it for the resulting array. Working with the values array gives you correct type hints and errors. We still expected the result list to be more narrow, only consisting of the exact values.
Let’s narrow the type of the values array even more to the specific enum values. Narrowing the types is helpful for IntelliSense in your editor, for example when doing comparisons. You can create a type for the exact values using a type literal. The type literal creates a union type with the exact enum values:
export enum FutureStudioBooks {
retrofit = 'Retrofit Book',
picasso = 'Picasso Book',
glide = 'Glide Book',
gson = 'Gson Book',
'gson-workbook' = 'Gson Workbook',
}
type FutureStudioBooksValues = `${FutureStudioBooks}`
// type FutureStudioBooksValues = "Retrofit Book" | "Picasso Book" | "Glide Book" | "Gson Book" | "Gson Workbook"
const values = Object.values<FutureStudioBooksValues>(FutureStudioBooks)
// const values: ("Retrofit Book" | "Picasso Book" | "Glide Book" | "Gson Book" | "Gson Workbook")[]
The type literal also works for a numeric enum
:
export enum FutureStudioBooks {
retrofit = 1,
picasso = 2,
glide = 3,
gson = 4,
'gson-workbook' = 5,
}
type FutureStudioBooksNumericValues = `${FutureStudioBooks}`
// type FutureStudioBooksNumericValues = "1" | "2" | "3" | "4" | "5"
Enjoy!