Skip to content

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 error
const bar: MyEnum = something; // error
const baz = asMyEnum(something); // error

Here, 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.

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.

int

CardType


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.

int

CollectibleType


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.

number

float


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.

number

int


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.

int

LevelStage


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.

int

NPCState


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.

number

number


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.

int

PillColor


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.

int

PillEffect


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.

int

PlayerType


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.

int

RoomType


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.

string

string


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.

int

TrinketType


isBoolean(variable): variable is boolean

Defined in: packages/isaacscript-common/src/functions/types.ts:173

unknown

variable is boolean


isFunction(variable): variable is Function

Defined in: packages/isaacscript-common/src/functions/types.ts:178

unknown

variable is Function


isInteger(variable): variable is int

Defined in: packages/isaacscript-common/src/functions/types.ts:182

unknown

variable is int


isNumber(variable): variable is number

Defined in: packages/isaacscript-common/src/functions/types.ts:192

unknown

variable is number


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.

unknown

variable is string | number | boolean


isString(variable): variable is string

Defined in: packages/isaacscript-common/src/functions/types.ts:208

unknown

variable is string


isTable(variable): variable is LuaMap<AnyNotNil, unknown>

Defined in: packages/isaacscript-common/src/functions/types.ts:212

unknown

variable is LuaMap<AnyNotNil, unknown>


isUserdata(variable): variable is LuaUserdata

Defined in: packages/isaacscript-common/src/functions/types.ts:219

unknown

variable is LuaUserdata


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.

string

int | undefined