From 327a9385edbdec572c2bb320530f5984c863ad35 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Wed, 23 Aug 2023 16:52:16 +0000 Subject: [PATCH] fix: Use intersection of matching types in AssociatedTypes<> lookup (#14) Resolves #11. Reviewed-on: https://code.studioinfinity.org/glen/typocomath/pulls/14 Co-authored-by: Glen Whitney Co-committed-by: Glen Whitney --- 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