Enums
Type Aliases
Section titled “Type Aliases”TranspiledEnum
Section titled “TranspiledEnum”TranspiledEnum =
Record<string,string|number|BitFlag|BitFlag128>
Defined in: packages/isaacscript-common/src/functions/enums.ts:11
In Lua, tables can have number keys, but since this is a type only being validated in TypeScript, we can match the JavaScript definition, meaning that we can omit the number from the keys.
Functions
Section titled “Functions”getEnumEntries()
Section titled “getEnumEntries()”getEnumEntries<
T>(transpiledEnum): readonly [string,T[keyofT]][]
Defined in: packages/isaacscript-common/src/functions/enums.ts:34
TypeScriptToLua will transpile TypeScript number enums to Lua tables that have a double mapping. Thus, when you iterate over them, you will get both the names of the enums and the values of the enums, in a random order. Use this helper function to get the entries of the enum with the reverse mappings filtered out.
This function will return the enum values in a sorted order, which may not necessarily be the same order as which they were declared in. (It is impossible to get the declaration order at run-time.)
This function will work properly for both number enums and string enums. (Reverse mappings are not created for string enums.)
Also see the getEnumKeys and getEnumValues helper functions.
For a more in depth explanation, see: https://isaacscript.github.io/main/gotchas#iterating-over-enums
Type Parameters
Section titled “Type Parameters”T extends TranspiledEnum
Parameters
Section titled “Parameters”transpiledEnum
Section titled “transpiledEnum”T
Returns
Section titled “Returns”readonly [string, T[keyof T]][]
getEnumKeys()
Section titled “getEnumKeys()”getEnumKeys(
transpiledEnum): readonlystring[]
Defined in: packages/isaacscript-common/src/functions/enums.ts:72
TypeScriptToLua will transpile TypeScript number enums to Lua tables that have a double mapping. Thus, when you iterate over them, you will get both the names of the enums and the values of the enums, in a random order. If all you need are the keys of an enum, use this helper function.
This function will return the enum keys in a sorted order, which may not necessarily be the same order as which they were declared in. (It is impossible to get the declaration order at run-time.)
This function will work properly for both number enums and string enums. (Reverse mappings are not created for string enums.)
Also see the getEnumEntries and getEnumValues helper functions.
For a more in depth explanation, see: https://isaacscript.github.io/main/gotchas#iterating-over-enums
Parameters
Section titled “Parameters”transpiledEnum
Section titled “transpiledEnum”Returns
Section titled “Returns”readonly string[]
getEnumLength()
Section titled “getEnumLength()”getEnumLength(
transpiledEnum):int
Defined in: packages/isaacscript-common/src/functions/enums.ts:78
Helper function to get the amount of entries inside of an enum.
Parameters
Section titled “Parameters”transpiledEnum
Section titled “transpiledEnum”Returns
Section titled “Returns”int
getEnumNames()
Section titled “getEnumNames()”getEnumNames(
transpiledEnum): readonlystring[]
Defined in: packages/isaacscript-common/src/functions/enums.ts:100
TypeScriptToLua will transpile TypeScript number enums to Lua tables that have a double mapping. Thus, when you iterate over them, you will get both the names of the enums and the values of the enums, in a random order. If all you need are the names of an enum from the reverse mapping, use this helper function.
This function will return the enum names in a sorted order, which may not necessarily be the same order as which they were declared in. (It is impossible to get the declaration order at run-time.)
This function will work properly for both number enums and string enums. (Reverse mappings are
not created for string enums, so their names would be equivalent to what would be returned by the
getEnumKeys function.)
For a more in depth explanation, see: https://isaacscript.github.io/main/gotchas#iterating-over-enums
Parameters
Section titled “Parameters”transpiledEnum
Section titled “transpiledEnum”Returns
Section titled “Returns”readonly string[]
getEnumValues()
Section titled “getEnumValues()”getEnumValues<
T>(transpiledEnum): readonlyT[keyofT][]
Defined in: packages/isaacscript-common/src/functions/enums.ts:134
TypeScriptToLua will transpile TypeScript number enums to Lua tables that have a double mapping. Thus, when you iterate over them, you will get both the names of the enums and the values of the enums, in a random order. If all you need are the values of an enum, use this helper function.
This function will return the enum values in a sorted order, which may not necessarily be the same order as which they were declared in. (It is impossible to get the declaration order at run-time.)
This function will work properly for both number enums and string enums. (Reverse mappings are not created for string enums.)
Also see the getEnumEntries and getEnumKeys helper functions.
For a more in depth explanation, see: https://isaacscript.github.io/main/gotchas#iterating-over-enums
Type Parameters
Section titled “Type Parameters”T extends TranspiledEnum
Parameters
Section titled “Parameters”transpiledEnum
Section titled “transpiledEnum”T
Returns
Section titled “Returns”readonly T[keyof T][]
getHighestEnumValue()
Section titled “getHighestEnumValue()”getHighestEnumValue<
T>(transpiledEnum):T[keyofT]
Defined in: packages/isaacscript-common/src/functions/enums.ts:149
Helper function to get the enum value with the highest value.
Note that this is not necessarily the enum value that is declared last in the code, since there is no way to infer that at run-time.
Throws an error if the provided enum is empty.
Type Parameters
Section titled “Type Parameters”T extends TranspiledEnum
Parameters
Section titled “Parameters”transpiledEnum
Section titled “transpiledEnum”T
Returns
Section titled “Returns”T[keyof T]
getLowestEnumValue()
Section titled “getLowestEnumValue()”getLowestEnumValue<
T>(transpiledEnum):T[keyofT]
Defined in: packages/isaacscript-common/src/functions/enums.ts:171
Helper function to get the enum value with the lowest value.
Note that this is not necessarily the enum value that is declared first in the code, since there is no way to infer that at run-time.
Throws an error if the provided enum is empty.
Type Parameters
Section titled “Type Parameters”T extends TranspiledEnum
Parameters
Section titled “Parameters”transpiledEnum
Section titled “transpiledEnum”T
Returns
Section titled “Returns”T[keyof T]
getRandomEnumValue()
Section titled “getRandomEnumValue()”getRandomEnumValue<
T>(transpiledEnum,seedOrRNG,exceptions?):T[keyofT]
Defined in: packages/isaacscript-common/src/functions/enums.ts:196
Helper function to get a random value from the provided enum.
If you want an unseeded value, you must explicitly pass undefined to the seedOrRNG parameter.
Type Parameters
Section titled “Type Parameters”T extends TranspiledEnum
Parameters
Section titled “Parameters”transpiledEnum
Section titled “transpiledEnum”T
The enum to get the value from.
seedOrRNG
Section titled “seedOrRNG”RNG | Seed | undefined
The Seed or RNG object to use. If an RNG object is provided, the
RNG.Next method will be called. If undefined is provided, it will default to
a random seed.
exceptions?
Section titled “exceptions?”readonly T[keyof T][] = []
Optional. An array of elements to skip over if selected.
Returns
Section titled “Returns”T[keyof T]
interfaceSatisfiesEnum()
Section titled “interfaceSatisfiesEnum()”interfaceSatisfiesEnum<
T,Enum>():void
Defined in: packages/isaacscript-common/src/functions/enums.ts:232
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
Section titled “Type Parameters”T extends Record<Enum, unknown>
Enum extends string | number
Returns
Section titled “Returns”void
isEnumValue()
Section titled “isEnumValue()”isEnumValue<
T>(value,transpiledEnum):value is T[keyof T]
Defined in: packages/isaacscript-common/src/functions/enums.ts:239
Helper function to validate that a particular value exists inside of an enum.
Type Parameters
Section titled “Type Parameters”T extends TranspiledEnum
Parameters
Section titled “Parameters”string | number | BitFlag | BitFlag128
transpiledEnum
Section titled “transpiledEnum”T
Returns
Section titled “Returns”value is T[keyof T]
validateCustomEnum()
Section titled “validateCustomEnum()”validateCustomEnum(
transpiledEnumName,transpiledEnum):void
Defined in: packages/isaacscript-common/src/functions/enums.ts:262
Helper function to check every value of a custom enum for -1. Will throw an run-time error if any -1 values are found. This is helpful because many methods of the Isaac class return -1 if they fail.
For example:
enum EntityTypeCustom { FOO = Isaac.GetEntityTypeByName("Foo"),}
validateCustomEnum("EntityTypeCustom", EntityTypeCustom);Parameters
Section titled “Parameters”transpiledEnumName
Section titled “transpiledEnumName”string
transpiledEnum
Section titled “transpiledEnum”Returns
Section titled “Returns”void
validateEnumContiguous()
Section titled “validateEnumContiguous()”validateEnumContiguous(
transpiledEnumName,transpiledEnum):void
Defined in: packages/isaacscript-common/src/functions/enums.ts:280
Helper function to validate if every value in a number enum is contiguous, starting at 0.
This is useful to automate checking large enums for typos.
Parameters
Section titled “Parameters”transpiledEnumName
Section titled “transpiledEnumName”string
transpiledEnum
Section titled “transpiledEnum”Returns
Section titled “Returns”void
