fix: Use intersection of matching types in AssociatedTypes<> lookup
This commit is contained in:
parent
76e144bc2a
commit
742ef55108
@ -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
|
||||
|
@ -8,7 +8,7 @@ export const number_type = {
|
||||
|
||||
declare module "../interfaces/type" {
|
||||
interface AssociatedTypes<T> {
|
||||
numbers: {
|
||||
number: {
|
||||
type: number
|
||||
zero: 0
|
||||
one: 1
|
||||
|
Loading…
Reference in New Issue
Block a user