fix: Use intersection of matching types in AssociatedTypes<> lookup (#14)

Resolves #11.
Reviewed-on: #14
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
This commit is contained in:
Glen Whitney 2023-08-23 16:52:16 +00:00 committed by Glen Whitney
parent 76e144bc2a
commit 327a9385ed
2 changed files with 8 additions and 4 deletions

View File

@ -13,6 +13,10 @@
* but that's OK, the generic parameter doesn't hurt in those cases.
****/
type ValueIntersectionByKeyUnion<T, TKey extends keyof T> = {
[P in TKey]: (k: T[P])=>void
} [TKey] extends ((k: infer I)=>void) ? I : never
export interface AssociatedTypes<T> {
undefined: {
type: undefined
@ -24,10 +28,10 @@ export interface AssociatedTypes<T> {
}
type AssociatedTypeNames = keyof AssociatedTypes<unknown>['undefined']
type ALookup<T, Name extends AssociatedTypeNames> = {
type ALookup<T, Name extends AssociatedTypeNames> = ValueIntersectionByKeyUnion<{
[K in keyof AssociatedTypes<T>]:
T extends AssociatedTypes<T>[K]['type'] ? AssociatedTypes<T>[K][Name] : never
}[keyof AssociatedTypes<T>]
T extends AssociatedTypes<T>[K]['type'] ? AssociatedTypes<T>[K][Name] : unknown},
keyof AssociatedTypes<T>>
// For everything to compile, zero and one must be subtypes of T:
export type ZeroType<T> = ALookup<T, 'zero'> & T

View File

@ -8,7 +8,7 @@ export const number_type = {
declare module "../interfaces/type" {
interface AssociatedTypes<T> {
numbers: {
number: {
type: number
zero: 0
one: 1