UNPKG

isaacscript-common

Version:

Helper functions and features for IsaacScript mods.

140 lines 6.54 kB
/** * Consider the following code that uses a number enum: * * ```ts * 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. * * @module */ import type { CardType, CollectibleType, LevelStage, NPCState, PillColor, PillEffect, PlayerType, RoomType, TrinketType } from "isaac-typescript-definitions"; /** * 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. */ export declare function asCardType(num: int): CardType; /** * 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. */ export declare function asCollectibleType(num: int): CollectibleType; /** * 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. */ export declare function asFloat(num: number): 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 "complete/strict-enums" ESLint rule. */ export declare function asInt(num: number): int; /** * 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. */ export declare function asLevelStage(num: int): LevelStage; /** * 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. */ export declare function asNPCState(num: int): NPCState; /** * 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. */ export declare function asNumber(num: number): number; /** * 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. */ export declare function asPillColor(num: int): PillColor; /** * 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. */ export declare function asPillEffect(num: int): PillEffect; /** * 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. */ export declare function asPlayerType(num: int): PlayerType; /** * 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. */ export declare function asRoomType(num: int): RoomType; /** * 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. */ export declare function asString(str: string): string; /** * 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. */ export declare function asTrinketType(num: int): TrinketType; export declare function isBoolean(variable: unknown): variable is boolean; export declare function isFunction(variable: unknown): variable is Function; export declare function isInteger(variable: unknown): variable is int; export declare function isNumber(variable: unknown): variable is number; /** Helper function to detect if a variable is a boolean, number, or string. */ export declare function isPrimitive(variable: unknown): variable is boolean | number | string; export declare function isString(variable: unknown): variable is string; export declare function isTable(variable: unknown): variable is LuaMap<AnyNotNil, unknown>; export declare function isUserdata(variable: unknown): variable is LuaUserdata; /** * 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`. */ export declare function parseIntSafe(string: string): int | undefined; //# sourceMappingURL=types.d.ts.map