Types
Consider the following code that uses a number enum:
enum MyEnum { Value1,}
function asMyEnum(num: number): MyEnum {}
declare const something: unknown;
const foo = something as MyEnum; // no errorconst bar: MyEnum = something; // errorconst baz = asMyEnum(something); // errorHere, using as does not give an error because TypeScript allows you to assert a type to a
supertype or a subtype. Thus, using as to perform a type assertion is not as safe as using a
variable declaration or a helper function. However, if we use a variable declaration, then the
complete/strict-enums rule is triggered, which requires suppressing the lint rule with a // eslint-disable-next-line. Thus, the safest and more concise way to do a type assertion is to use
a helper function.
This file contains helper functions for various number enums that might require type assertions. It also contains helper functions for run-time type checks.
Functions
Section titled “Functions”asCardType()
Section titled “asCardType()”asCardType(
num):CardType
Defined in: packages/isaacscript-common/src/functions/types.ts:49
Helper function to safely cast an int to a CardType. (This is better than using the as
TypeScript keyword to do a type assertion, since that can obfuscate compiler errors. )
This is useful to satisfy the “complete/strict-enums” ESLint rule.
Parameters
Section titled “Parameters”int
Returns
Section titled “Returns”CardType
asCollectibleType()
Section titled “asCollectibleType()”asCollectibleType(
num):CollectibleType
Defined in: packages/isaacscript-common/src/functions/types.ts:59
Helper function to safely cast an int to a CollectibleType. (This is better than using the
as TypeScript keyword to do a type assertion, since that can obfuscate compiler errors. )
This is useful to satisfy the “complete/strict-enums” ESLint rule.
Parameters
Section titled “Parameters”int
Returns
Section titled “Returns”CollectibleType
asFloat()
Section titled “asFloat()”asFloat(
num):float
Defined in: packages/isaacscript-common/src/functions/types.ts:69
Helper function to safely cast an enum to an int. (This is better than using the as
TypeScript keyword to do a type assertion, since that can obfuscate compiler errors. )
This is useful to satisfy the “complete/strict-enums” ESLint rule.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”float
asInt()
Section titled “asInt()”asInt(
num):int
Defined in: packages/isaacscript-common/src/functions/types.ts:79
Helper function to safely cast an enum to an int. (This is better than using the as
TypeScript keyword to do a type assertion, since that can obfuscate compiler errors. )
This is useful to satisfy the “complete/strict-enums” ESLint rule.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”int
asLevelStage()
Section titled “asLevelStage()”asLevelStage(
num):LevelStage
Defined in: packages/isaacscript-common/src/functions/types.ts:89
Helper function to safely cast an int to a LevelStage. (This is better than using the as
TypeScript keyword to do a type assertion, since that can obfuscate compiler errors. )
This is useful to satisfy the “complete/strict-enums” ESLint rule.
Parameters
Section titled “Parameters”int
Returns
Section titled “Returns”LevelStage
asNPCState()
Section titled “asNPCState()”asNPCState(
num):NPCState
Defined in: packages/isaacscript-common/src/functions/types.ts:99
Helper function to safely cast an int to a NPCState. (This is better than using the as
TypeScript keyword to do a type assertion, since that can obfuscate compiler errors. )
This is useful to satisfy the “complete/strict-enums” ESLint rule.
Parameters
Section titled “Parameters”int
Returns
Section titled “Returns”NPCState
asNumber()
Section titled “asNumber()”asNumber(
num):number
Defined in: packages/isaacscript-common/src/functions/types.ts:109
Helper function to safely cast an enum to a number. (This is better than using the as
TypeScript keyword to do a type assertion, since that can obfuscate compiler errors. )
This is useful to satisfy the “complete/strict-enums” ESLint rule.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”number
asPillColor()
Section titled “asPillColor()”asPillColor(
num):PillColor
Defined in: packages/isaacscript-common/src/functions/types.ts:119
Helper function to safely cast an int to a PillColor. (This is better than using the as
TypeScript keyword to do a type assertion, since that can obfuscate compiler errors. )
This is useful to satisfy the “complete/strict-enums” ESLint rule.
Parameters
Section titled “Parameters”int
Returns
Section titled “Returns”PillColor
asPillEffect()
Section titled “asPillEffect()”asPillEffect(
num):PillEffect
Defined in: packages/isaacscript-common/src/functions/types.ts:129
Helper function to safely cast an int to a PillEffect. (This is better than using the as
TypeScript keyword to do a type assertion, since that can obfuscate compiler errors. )
This is useful to satisfy the “complete/strict-enums” ESLint rule.
Parameters
Section titled “Parameters”int
Returns
Section titled “Returns”PillEffect
asPlayerType()
Section titled “asPlayerType()”asPlayerType(
num):PlayerType
Defined in: packages/isaacscript-common/src/functions/types.ts:139
Helper function to safely cast an int to a PlayerType. (This is better than using the as
TypeScript keyword to do a type assertion, since that can obfuscate compiler errors. )
This is useful to satisfy the “complete/strict-enums” ESLint rule.
Parameters
Section titled “Parameters”int
Returns
Section titled “Returns”PlayerType
asRoomType()
Section titled “asRoomType()”asRoomType(
num):RoomType
Defined in: packages/isaacscript-common/src/functions/types.ts:149
Helper function to safely cast an int to a RoomType. (This is better than using the as
TypeScript keyword to do a type assertion, since that can obfuscate compiler errors. )
This is useful to satisfy the “complete/strict-enums” ESLint rule.
Parameters
Section titled “Parameters”int
Returns
Section titled “Returns”RoomType
asString()
Section titled “asString()”asString(
str):string
Defined in: packages/isaacscript-common/src/functions/types.ts:159
Helper function to safely cast an enum to a string. (This is better than using the as
TypeScript keyword to do a type assertion, since that can obfuscate compiler errors. )
This is useful to satisfy the “complete/strict-enums” ESLint rule.
Parameters
Section titled “Parameters”string
Returns
Section titled “Returns”string
asTrinketType()
Section titled “asTrinketType()”asTrinketType(
num):TrinketType
Defined in: packages/isaacscript-common/src/functions/types.ts:169
Helper function to safely cast an int to a TrinketType. (This is better than using the as
TypeScript keyword to do a type assertion, since that can obfuscate compiler errors. )
This is useful to satisfy the “complete/strict-enums” ESLint rule.
Parameters
Section titled “Parameters”int
Returns
Section titled “Returns”TrinketType
isBoolean()
Section titled “isBoolean()”isBoolean(
variable):variable is boolean
Defined in: packages/isaacscript-common/src/functions/types.ts:173
Parameters
Section titled “Parameters”variable
Section titled “variable”unknown
Returns
Section titled “Returns”variable is boolean
isFunction()
Section titled “isFunction()”isFunction(
variable):variable is Function
Defined in: packages/isaacscript-common/src/functions/types.ts:178
Parameters
Section titled “Parameters”variable
Section titled “variable”unknown
Returns
Section titled “Returns”variable is Function
isInteger()
Section titled “isInteger()”isInteger(
variable):variable is int
Defined in: packages/isaacscript-common/src/functions/types.ts:182
Parameters
Section titled “Parameters”variable
Section titled “variable”unknown
Returns
Section titled “Returns”variable is int
isNumber()
Section titled “isNumber()”isNumber(
variable):variable is number
Defined in: packages/isaacscript-common/src/functions/types.ts:192
Parameters
Section titled “Parameters”variable
Section titled “variable”unknown
Returns
Section titled “Returns”variable is number
isPrimitive()
Section titled “isPrimitive()”isPrimitive(
variable): variable is string | number | boolean
Defined in: packages/isaacscript-common/src/functions/types.ts:197
Helper function to detect if a variable is a boolean, number, or string.
Parameters
Section titled “Parameters”variable
Section titled “variable”unknown
Returns
Section titled “Returns”variable is string | number | boolean
isString()
Section titled “isString()”isString(
variable):variable is string
Defined in: packages/isaacscript-common/src/functions/types.ts:208
Parameters
Section titled “Parameters”variable
Section titled “variable”unknown
Returns
Section titled “Returns”variable is string
isTable()
Section titled “isTable()”isTable(
variable):variable is LuaMap<AnyNotNil, unknown>
Defined in: packages/isaacscript-common/src/functions/types.ts:212
Parameters
Section titled “Parameters”variable
Section titled “variable”unknown
Returns
Section titled “Returns”variable is LuaMap<AnyNotNil, unknown>
isUserdata()
Section titled “isUserdata()”isUserdata(
variable):variable is LuaUserdata
Defined in: packages/isaacscript-common/src/functions/types.ts:219
Parameters
Section titled “Parameters”variable
Section titled “variable”unknown
Returns
Section titled “Returns”variable is LuaUserdata
parseIntSafe()
Section titled “parseIntSafe()”parseIntSafe(
string):int|undefined
Defined in: packages/isaacscript-common/src/functions/types.ts:232
Helper function to convert a string to an integer. Returns undefined if the string is not an integer.
Under the hood, this uses the built-in tonumber and math.floor functions.
This is named parseIntSafe in order to match the helper function from complete-common.
Parameters
Section titled “Parameters”string
Section titled “string”string
Returns
Section titled “Returns”int | undefined
