Enums
Helper functions that have to do with TypeScript enums.
Functions
getEnumEntries()
function getEnumEntries<T>(transpiledEnum): ReadonlyArray<[string, T[keyof T]]>
Helper function to get the entries of an enum.
(By default, TypeScript will put the keys inside of the values of a number-based enum, so those have to be filtered out.)
This function will work properly for both number and string enums.
Type Parameters
Type Parameter |
---|
T extends TranspiledEnum |
Parameters
Parameter | Type |
---|---|
transpiledEnum | T |
Returns
ReadonlyArray
<[string
, T
[keyof T
]]>
Defined in
getEnumKeys()
function getEnumKeys(transpiledEnum): readonly string[]
Helper function to get the keys of an enum.
(By default, TypeScript will put the keys inside of the values of a number-based enum, so those have to be filtered out.)
This function will work properly for both number and string enums.
Parameters
Parameter | Type |
---|---|
transpiledEnum | TranspiledEnum |
Returns
readonly string
[]
Defined in
getEnumValues()
function getEnumValues<T>(transpiledEnum): ReadonlyArray<T[keyof T]>
Helper function to get the only the values of an enum.
(By default, TypeScript will put the keys inside of the values of a number-based enum, so those have to be filtered out.)
This function will work properly for both number and string enums.
Type Parameters
Type Parameter |
---|
T extends TranspiledEnum |
Parameters
Parameter | Type |
---|---|
transpiledEnum | T |
Returns
ReadonlyArray
<T
[keyof T
]>
Defined in
interfaceSatisfiesEnum()
function interfaceSatisfiesEnum<T, Enum>(): void
Helper function to validate that an interface contains all of the keys of an enum. You must specify both generic parameters in order for this to work properly (i.e. the interface and then the enum).
For example:
enum MyEnum {
Value1,
Value2,
Value3,
}
interface MyEnumToType {
[MyEnum.Value1]: boolean;
[MyEnum.Value2]: number;
[MyEnum.Value3]: string;
}
interfaceSatisfiesEnum<MyEnumToType, MyEnum>();
This function is only meant to be used with interfaces (i.e. types that will not exist at
run-time). If you are generating an object that will contain all of the keys of an enum, use the
satisfies
operator with the Record
type instead.
Type Parameters
Type Parameter |
---|
T extends Record <Enum , unknown > |
Enum extends string | number |
Returns
void
Defined in
isEnumValue()
function isEnumValue<T>(
value,
transpiledEnum,
set?): value is T[keyof T]
Helper function to validate that a particular value exists inside of an enum.
Type Parameters
Type Parameter |
---|
T extends TranspiledEnum |
Parameters
Parameter | Type | Description |
---|---|---|
value | string | number | The value to check. |
transpiledEnum | T | The enum to check against. |
set ? | ReadonlySet <string | number > | Optional. A set that contains all of the values of an enum. If provided, this function will check for existence using the set (instead of the enum itself). Using a set should be more performant for enums with around 52 or more elements. |
Returns
value is T[keyof T]