From 742ef551085596b847a9d25d7d7863f47aa7f9f8 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Wed, 23 Aug 2023 09:49:48 -0700 Subject: [PATCH] fix: Use intersection of matching types in AssociatedTypes<> lookup --- src/interfaces/type.ts | 10 +++++++--- src/numbers/type.ts | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/interfaces/type.ts b/src/interfaces/type.ts index 9e6870d..9fb1163 100644 --- a/src/interfaces/type.ts +++ b/src/interfaces/type.ts @@ -13,6 +13,10 @@ * but that's OK, the generic parameter doesn't hurt in those cases. ****/ +type ValueIntersectionByKeyUnion = { + [P in TKey]: (k: T[P])=>void +} [TKey] extends ((k: infer I)=>void) ? I : never + export interface AssociatedTypes { undefined: { type: undefined @@ -24,10 +28,10 @@ export interface AssociatedTypes { } type AssociatedTypeNames = keyof AssociatedTypes['undefined'] -type ALookup = { +type ALookup = ValueIntersectionByKeyUnion<{ [K in keyof AssociatedTypes]: - T extends AssociatedTypes[K]['type'] ? AssociatedTypes[K][Name] : never -}[keyof AssociatedTypes] + T extends AssociatedTypes[K]['type'] ? AssociatedTypes[K][Name] : unknown}, + keyof AssociatedTypes> // For everything to compile, zero and one must be subtypes of T: export type ZeroType = ALookup & T diff --git a/src/numbers/type.ts b/src/numbers/type.ts index 11e5d1c..48fb95e 100644 --- a/src/numbers/type.ts +++ b/src/numbers/type.ts @@ -8,7 +8,7 @@ export const number_type = { declare module "../interfaces/type" { interface AssociatedTypes { - numbers: { + number: { type: number zero: 0 one: 1