Skip to content

Enums

Helper functions that have to do with TypeScript enums.

function getEnumEntries<T>(transpiledEnum): readonly [string, T[keyof T]][];

Defined in: functions/enums.ts:17

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 Parameter
T extends TranspiledEnum
Parameter Type
transpiledEnum T

readonly [string, T[keyof T]][]


function getEnumKeys(transpiledEnum): readonly string[];

Defined in: functions/enums.ts:38

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.

Parameter Type
transpiledEnum TranspiledEnum

readonly string[]


function getEnumValues<T>(transpiledEnum): readonly T[keyof T][];

Defined in: functions/enums.ts:51

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 Parameter
T extends TranspiledEnum
Parameter Type
transpiledEnum T

readonly T[keyof T][]


function interfaceSatisfiesEnum<T, Enum>(): void;

Defined in: functions/enums.ts:85

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 Parameter
T extends Record<Enum, unknown>
Enum extends string | number

void


function isEnumValue<T>(value, transpiledEnum, set?): value is T[keyof T];

Defined in: functions/enums.ts:100

Helper function to validate that a particular value exists inside of an enum.

Type Parameter
T extends TranspiledEnum
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.

value is T[keyof T]