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

Merged
glen merged 1 commits from fix_associated into main 2023-08-23 16:52:17 +00:00
2 changed files with 8 additions and 4 deletions
Showing only changes of commit 742ef55108 - Show all commits

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