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
isaacscript/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
asCardType
▸ asCardType(num): CardType
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 "isaacscript/strict-enums" ESLint rule.
Parameters
| Name | Type |
|---|---|
num | int |
Returns
CardType
Defined in
packages/isaacscript-common/src/functions/types.ts:49
asCollectibleType
▸ asCollectibleType(num): CollectibleType
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 "isaacscript/strict-enums" ESLint rule.
Parameters
| Name | Type |
|---|---|
num | int |
Returns
CollectibleType
Defined in
packages/isaacscript-common/src/functions/types.ts:59
asFloat
▸ asFloat(num): float
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 "isaacscript/strict-enums" ESLint rule.
Parameters
| Name | Type |
|---|---|
num | number |
Returns
float
Defined in
packages/isaacscript-common/src/functions/types.ts:69
asInt
▸ asInt(num): int
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 "isaacscript/strict-enums" ESLint rule.
Parameters
| Name | Type |
|---|---|
num | number |
Returns
int
Defined in
packages/isaacscript-common/src/functions/types.ts:79
asLevelStage
▸ asLevelStage(num): LevelStage
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 "isaacscript/strict-enums" ESLint rule.
Parameters
| Name | Type |
|---|---|
num | int |
Returns
LevelStage
Defined in
packages/isaacscript-common/src/functions/types.ts:89
asNPCState
▸ asNPCState(num): NPCState
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 "isaacscript/strict-enums" ESLint rule.
Parameters
| Name | Type |
|---|---|
num | int |
Returns
NPCState
Defined in
packages/isaacscript-common/src/functions/types.ts:99
asNumber
▸ asNumber(num): number
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 "isaacscript/strict-enums" ESLint rule.
Parameters
| Name | Type |
|---|---|
num | number |
Returns
number
Defined in
packages/isaacscript-common/src/functions/types.ts:109
asPillColor
▸ asPillColor(num): PillColor
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 "isaacscript/strict-enums" ESLint rule.
Parameters
| Name | Type |
|---|---|
num | int |
Returns
PillColor
Defined in
packages/isaacscript-common/src/functions/types.ts:119
asPillEffect
▸ asPillEffect(num): PillEffect
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 "isaacscript/strict-enums" ESLint rule.
Parameters
| Name | Type |
|---|---|
num | int |
Returns
PillEffect
Defined in
packages/isaacscript-common/src/functions/types.ts:129
asPlayerType
▸ asPlayerType(num): PlayerType
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 "isaacscript/strict-enums" ESLint rule.
Parameters
| Name | Type |
|---|---|
num | int |
Returns
PlayerType
Defined in
packages/isaacscript-common/src/functions/types.ts:139
asRoomType
▸ asRoomType(num): RoomType
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 "isaacscript/strict-enums" ESLint rule.
Parameters
| Name | Type |
|---|---|
num | int |
Returns
RoomType
Defined in
packages/isaacscript-common/src/functions/types.ts:149
asString
▸ asString(str): string
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 "isaacscript/strict-enums" ESLint rule.
Parameters
| Name | Type |
|---|---|
str | string |
Returns
string
Defined in
packages/isaacscript-common/src/functions/types.ts:159
asTrinketType
▸ asTrinketType(num): TrinketType
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 "isaacscript/strict-enums" ESLint rule.
Parameters
| Name | Type |
|---|---|
num | int |
Returns
TrinketType
Defined in
packages/isaacscript-common/src/functions/types.ts:169
isBoolean
▸ isBoolean(variable): variable is boolean
Parameters
| Name | Type |
|---|---|
variable | unknown |
Returns
variable is boolean
Defined in
packages/isaacscript-common/src/functions/types.ts:173
isFunction
▸ isFunction(variable): variable is Function
Parameters
| Name | Type |
|---|---|
variable | unknown |
Returns
variable is Function
Defined in
packages/isaacscript-common/src/functions/types.ts:178
isInteger
▸ isInteger(variable): variable is int
Parameters
| Name | Type |
|---|---|
variable | unknown |
Returns
variable is int
Defined in
packages/isaacscript-common/src/functions/types.ts:182
isNumber
▸ isNumber(variable): variable is number
Parameters
| Name | Type |
|---|---|
variable | unknown |
Returns
variable is number
Defined in
packages/isaacscript-common/src/functions/types.ts:190
isPrimitive
▸ isPrimitive(variable): variable is string | number | boolean
Helper function to detect if a variable is a boolean, number, or string.
Parameters
| Name | Type |
|---|---|
variable | unknown |
Returns
variable is string | number | boolean
Defined in
packages/isaacscript-common/src/functions/types.ts:195
isString
▸ isString(variable): variable is string
Parameters
| Name | Type |
|---|---|
variable | unknown |
Returns
variable is string
Defined in
packages/isaacscript-common/src/functions/types.ts:206
isTable
▸ isTable(variable): variable is LuaMap<AnyNotNil, unknown>
Parameters
| Name | Type |
|---|---|
variable | unknown |
Returns
variable is LuaMap<AnyNotNil, unknown>
Defined in
packages/isaacscript-common/src/functions/types.ts:210
isUserdata
▸ isUserdata(variable): variable is LuaUserdata
Parameters
| Name | Type |
|---|---|
variable | unknown |
Returns
variable is LuaUserdata
Defined in
packages/isaacscript-common/src/functions/types.ts:217
parseIntSafe
▸ parseIntSafe(string): int | undefined
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 in isaacscript-common-ts.
Parameters
| Name | Type |
|---|---|
string | string |
Returns
int | undefined