From 1ae69a4183bca6deffbf0a2356a6b47477670d1b Mon Sep 17 00:00:00 2001 From: Bertie690 <136088738+Bertie690@users.noreply.github.com> Date: Wed, 30 Jul 2025 21:17:17 -0400 Subject: [PATCH] [Dev] Moved type helpers to separate directory; (#6123) * [Dev] Moved type helpers to separate directory; renamed `EnumValues` to `ObjectValues` and enforced usage * Update tsconfig.json Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> * Fixed import issue * Updated documentation slightly --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> Co-authored-by: Dean <69436131+emdeann@users.noreply.github.com> --- src/@types/arena-tags.ts | 2 +- src/@types/{ => helpers}/enum-types.ts | 10 ++--- src/@types/{ => helpers}/type-helpers.ts | 18 +++++--- src/@types/modifier-types.ts | 3 +- src/@types/phase-types.ts | 3 +- src/enums/ability-attr.ts | 4 +- src/enums/dex-attr.ts | 4 +- src/enums/gacha-types.ts | 4 +- src/enums/hit-check-result.ts | 4 +- src/utils/enums.ts | 6 +-- test/types/enum-types.test-d.ts | 53 +++++++++++++++--------- tsconfig.json | 2 +- 12 files changed, 70 insertions(+), 43 deletions(-) rename src/@types/{ => helpers}/enum-types.ts (68%) rename src/@types/{ => helpers}/type-helpers.ts (81%) diff --git a/src/@types/arena-tags.ts b/src/@types/arena-tags.ts index ab4339b2fef..9ccccb1df5c 100644 --- a/src/@types/arena-tags.ts +++ b/src/@types/arena-tags.ts @@ -1,6 +1,6 @@ import type { ArenaTagTypeMap } from "#data/arena-tag"; import type { ArenaTagType } from "#enums/arena-tag-type"; -import type { NonFunctionProperties } from "./type-helpers"; +import type { NonFunctionProperties } from "#types/type-helpers"; /** Subset of {@linkcode ArenaTagType}s that apply some negative effect to pokemon that switch in ({@link https://bulbapedia.bulbagarden.net/wiki/List_of_moves_that_cause_entry_hazards#List_of_traps | entry hazards} and Imprison. */ export type ArenaTrapTagType = diff --git a/src/@types/enum-types.ts b/src/@types/helpers/enum-types.ts similarity index 68% rename from src/@types/enum-types.ts rename to src/@types/helpers/enum-types.ts index 84df0a96505..2461f900c6b 100644 --- a/src/@types/enum-types.ts +++ b/src/@types/helpers/enum-types.ts @@ -1,18 +1,14 @@ +import type { ObjectValues } from "#types/type-helpers"; + /** Union type accepting any TS Enum or `const object`, with or without reverse mapping. */ export type EnumOrObject = Record; -/** - * Utility type to extract the enum values from a `const object`, - * or convert an `enum` interface produced by `typeof Enum` into the union type representing its values. - */ -export type EnumValues = E[keyof E]; - /** * Generic type constraint representing a TS numeric enum with reverse mappings. * @example * TSNumericEnum */ -export type TSNumericEnum = number extends EnumValues ? T : never; +export type TSNumericEnum = number extends ObjectValues ? T : never; /** Generic type constraint representing a non reverse-mapped TS enum or `const object`. */ export type NormalEnum = Exclude>; diff --git a/src/@types/type-helpers.ts b/src/@types/helpers/type-helpers.ts similarity index 81% rename from src/@types/type-helpers.ts rename to src/@types/helpers/type-helpers.ts index b3e5b1cfc07..37f97fcf08c 100644 --- a/src/@types/type-helpers.ts +++ b/src/@types/helpers/type-helpers.ts @@ -6,8 +6,6 @@ import type { AbAttr } from "#abilities/ability"; // biome-ignore-end lint/correctness/noUnusedImports: Used in a tsdoc comment -import type { EnumValues } from "#types/enum-types"; - /** * Exactly matches the type of the argument, preventing adding additional properties. * @@ -37,16 +35,25 @@ export type Mutable = { }; /** - * Type helper to obtain the keys associated with a given value inside a `const object`. + * Type helper to obtain the keys associated with a given value inside an object. * @typeParam O - The type of the object * @typeParam V - The type of one of O's values */ -export type InferKeys, V extends EnumValues> = { +export type InferKeys> = { [K in keyof O]: O[K] extends V ? K : never; }[keyof O]; /** - * Type helper that matches any `Function` type. Equivalent to `Function`, but will not raise a warning from Biome. + * Utility type to obtain the values of a given object. \ + * Functions similar to `keyof E`, except producing the values instead of the keys. + * @remarks + * This can be used to convert an `enum` interface produced by `typeof Enum` into the union type representing its members. + */ +export type ObjectValues = E[keyof E]; + +/** + * Type helper that matches any `Function` type. + * Equivalent to `Function`, but will not raise a warning from Biome. */ export type AnyFn = (...args: any[]) => any; @@ -65,6 +72,7 @@ export type NonFunctionProperties = { /** * Type helper to extract out non-function properties from a type, recursively applying to nested properties. + * This can be used to mimic the effects of JSON serialization and de-serialization on a given type. */ export type NonFunctionPropertiesRecursive = { [K in keyof Class as Class[K] extends AnyFn ? never : K]: Class[K] extends Array diff --git a/src/@types/modifier-types.ts b/src/@types/modifier-types.ts index 28b39d1a151..13a84a984e2 100644 --- a/src/@types/modifier-types.ts +++ b/src/@types/modifier-types.ts @@ -3,6 +3,7 @@ import type { Pokemon } from "#field/pokemon"; import type { ModifierConstructorMap } from "#modifiers/modifier"; import type { ModifierType, WeightedModifierType } from "#modifiers/modifier-type"; +import type { ObjectValues } from "#types/type-helpers"; export type ModifierTypeFunc = () => ModifierType; export type WeightedModifierTypeWeightFunc = (party: Pokemon[], rerollCount?: number) => number; @@ -19,7 +20,7 @@ export type ModifierInstanceMap = { /** * Union type of all modifier constructors. */ -export type ModifierClass = ModifierConstructorMap[keyof ModifierConstructorMap]; +export type ModifierClass = ObjectValues; /** * Union type of all modifier names as strings. diff --git a/src/@types/phase-types.ts b/src/@types/phase-types.ts index 1d68c7921dd..91673053747 100644 --- a/src/@types/phase-types.ts +++ b/src/@types/phase-types.ts @@ -1,4 +1,5 @@ import type { PhaseConstructorMap } from "#app/phase-manager"; +import type { ObjectValues } from "#types/type-helpers"; // Intentionally export the types of everything in phase-manager, as this file is meant to be // the centralized place for type definitions for the phase system. @@ -17,7 +18,7 @@ export type PhaseMap = { /** * Union type of all phase constructors. */ -export type PhaseClass = PhaseConstructorMap[keyof PhaseConstructorMap]; +export type PhaseClass = ObjectValues; /** * Union type of all phase names as strings. diff --git a/src/enums/ability-attr.ts b/src/enums/ability-attr.ts index 5f7d107f2d1..a3b9511ad02 100644 --- a/src/enums/ability-attr.ts +++ b/src/enums/ability-attr.ts @@ -1,3 +1,5 @@ +import type { ObjectValues } from "#types/type-helpers"; + /** * Not to be confused with an Ability Attribute. * This is an object literal storing the slot that an ability can occupy. @@ -8,4 +10,4 @@ export const AbilityAttr = Object.freeze({ ABILITY_HIDDEN: 4, }); -export type AbilityAttr = typeof AbilityAttr[keyof typeof AbilityAttr]; \ No newline at end of file +export type AbilityAttr = ObjectValues; \ No newline at end of file diff --git a/src/enums/dex-attr.ts b/src/enums/dex-attr.ts index ee5ceb43ef2..1a98167b4a1 100644 --- a/src/enums/dex-attr.ts +++ b/src/enums/dex-attr.ts @@ -1,3 +1,5 @@ +import type { ObjectValues } from "#types/type-helpers"; + export const DexAttr = Object.freeze({ NON_SHINY: 1n, SHINY: 2n, @@ -8,4 +10,4 @@ export const DexAttr = Object.freeze({ VARIANT_3: 64n, DEFAULT_FORM: 128n, }); -export type DexAttr = typeof DexAttr[keyof typeof DexAttr]; +export type DexAttr = ObjectValues; diff --git a/src/enums/gacha-types.ts b/src/enums/gacha-types.ts index cd0bc67eae0..08f147b27b1 100644 --- a/src/enums/gacha-types.ts +++ b/src/enums/gacha-types.ts @@ -1,7 +1,9 @@ +import type { ObjectValues } from "#types/type-helpers"; + export const GachaType = Object.freeze({ MOVE: 0, LEGENDARY: 1, SHINY: 2 }); -export type GachaType = typeof GachaType[keyof typeof GachaType]; +export type GachaType = ObjectValues; diff --git a/src/enums/hit-check-result.ts b/src/enums/hit-check-result.ts index cf8a2b17194..0866050341e 100644 --- a/src/enums/hit-check-result.ts +++ b/src/enums/hit-check-result.ts @@ -1,3 +1,5 @@ +import type { ObjectValues } from "#types/type-helpers"; + /** The result of a hit check calculation */ export const HitCheckResult = { /** Hit checks haven't been evaluated yet in this pass */ @@ -20,4 +22,4 @@ export const HitCheckResult = { ERROR: 8, } as const; -export type HitCheckResult = typeof HitCheckResult[keyof typeof HitCheckResult]; +export type HitCheckResult = ObjectValues; diff --git a/src/utils/enums.ts b/src/utils/enums.ts index 98cb4272ee9..25ee864794c 100644 --- a/src/utils/enums.ts +++ b/src/utils/enums.ts @@ -1,5 +1,5 @@ -import type { EnumOrObject, EnumValues, NormalEnum, TSNumericEnum } from "#app/@types/enum-types"; -import type { InferKeys } from "#app/@types/type-helpers"; +import type { EnumOrObject, NormalEnum, TSNumericEnum } from "#types/enum-types"; +import type { InferKeys, ObjectValues } from "#types/type-helpers"; /** * Return the string keys of an Enum object, excluding reverse-mapped numbers. @@ -61,7 +61,7 @@ export function getEnumValues(enumType: TSNumericEnum * If multiple keys map to the same value, the first one (in insertion order) will be retrieved, * but the return type will be the union of ALL their corresponding keys. */ -export function enumValueToKey>( +export function enumValueToKey>( object: NormalEnum, val: V, ): InferKeys { diff --git a/test/types/enum-types.test-d.ts b/test/types/enum-types.test-d.ts index 396c479e85a..3d03098c2ad 100644 --- a/test/types/enum-types.test-d.ts +++ b/test/types/enum-types.test-d.ts @@ -1,5 +1,6 @@ -import type { EnumOrObject, EnumValues, NormalEnum, TSNumericEnum } from "#app/@types/enum-types"; import type { enumValueToKey, getEnumKeys, getEnumValues } from "#app/utils/enums"; +import type { EnumOrObject, NormalEnum, TSNumericEnum } from "#types/enum-types"; +import type { ObjectValues } from "#types/type-helpers"; import { describe, expectTypeOf, it } from "vitest"; enum testEnumNum { @@ -16,21 +17,33 @@ const testObjNum = { testON1: 1, testON2: 2 } as const; const testObjString = { testOS1: "apple", testOS2: "banana" } as const; -describe("Enum Type Helpers", () => { - describe("EnumValues", () => { - it("should go from enum object type to value type", () => { - expectTypeOf>().toEqualTypeOf(); - expectTypeOf>().branded.toEqualTypeOf<1 | 2>(); +interface testObject { + key_1: "1"; + key_2: "2"; + key_3: "3"; +} - expectTypeOf>().toEqualTypeOf(); - expectTypeOf>().toEqualTypeOf(); - expectTypeOf>().toMatchTypeOf<"apple" | "banana">(); +describe("Enum Type Helpers", () => { + describe("ObjectValues", () => { + it("should produce a union of an object's values", () => { + expectTypeOf>().toEqualTypeOf<"1" | "2" | "3">(); + }); + + it("should go from enum object type to value type", () => { + expectTypeOf>().toEqualTypeOf(); + expectTypeOf>().branded.toEqualTypeOf<1 | 2>(); + + expectTypeOf>().toEqualTypeOf(); + expectTypeOf>().toEqualTypeOf< + testEnumString.testS1 | testEnumString.testS2 + >(); + + expectTypeOf>().toExtend<"apple" | "banana">(); }); it("should produce union of const object values as type", () => { - expectTypeOf>().toEqualTypeOf<1 | 2>(); - - expectTypeOf>().toEqualTypeOf<"apple" | "banana">(); + expectTypeOf>().toEqualTypeOf<1 | 2>(); + expectTypeOf>().toEqualTypeOf<"apple" | "banana">(); }); }); @@ -38,7 +51,6 @@ describe("Enum Type Helpers", () => { it("should match numeric enums", () => { expectTypeOf>().toEqualTypeOf(); }); - it("should not match string enums or const objects", () => { expectTypeOf>().toBeNever(); expectTypeOf>().toBeNever(); @@ -59,19 +71,19 @@ describe("Enum Type Helpers", () => { describe("EnumOrObject", () => { it("should match any enum or const object", () => { - expectTypeOf().toMatchTypeOf(); - expectTypeOf().toMatchTypeOf(); - expectTypeOf().toMatchTypeOf(); - expectTypeOf().toMatchTypeOf(); + expectTypeOf().toExtend(); + expectTypeOf().toExtend(); + expectTypeOf().toExtend(); + expectTypeOf().toExtend(); }); it("should not match an enum value union w/o typeof", () => { - expectTypeOf().not.toMatchTypeOf(); - expectTypeOf().not.toMatchTypeOf(); + expectTypeOf().not.toExtend(); + expectTypeOf().not.toExtend(); }); it("should be equivalent to `TSNumericEnum | NormalEnum`", () => { - expectTypeOf().branded.toEqualTypeOf | NormalEnum>(); + expectTypeOf().toEqualTypeOf | NormalEnum>(); }); }); }); @@ -80,6 +92,7 @@ describe("Enum Functions", () => { describe("getEnumKeys", () => { it("should retrieve keys of numeric enum", () => { expectTypeOf>().returns.toEqualTypeOf<("testN1" | "testN2")[]>(); + expectTypeOf>().returns.toEqualTypeOf<("testON1" | "testON2")[]>(); }); }); diff --git a/tsconfig.json b/tsconfig.json index 9aa06829789..dcbf7456df8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -49,7 +49,7 @@ "./system/*.ts" ], "#trainers/*": ["./data/trainers/*.ts"], - "#types/*": ["./@types/*.ts", "./typings/phaser/*.ts"], + "#types/*": ["./@types/helpers/*.ts", "./@types/*.ts", "./typings/phaser/*.ts"], "#ui/*": ["./ui/battle-info/*.ts", "./ui/settings/*.ts", "./ui/*.ts"], "#utils/*": ["./utils/*.ts"], "#data/*": ["./data/pokemon-forms/*.ts", "./data/pokemon/*.ts", "./data/*.ts"],