Last active
July 23, 2020 13:57
-
-
Save antfu/67dd0cbddcd9ecec643d414c2b93be96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { UnionToIntersection } from 'utility-types'; | |
type String<T> = T extends string ? T : any | |
type Keys<T, Field extends keyof T> = | |
T extends { [P in Field]: infer A } | |
? { [P in String<A>]: T } | |
: never | |
type LookupTable<Union, key extends keyof Union> = UnionToIntersection<Keys<Union, key>> | |
// usage | |
interface Slide { | |
name: 'slide'; | |
key: 'foo' | |
next(): void; | |
} | |
interface Pullup { | |
name: 'pullup'; | |
key: 'bar' | |
prev(): void; | |
} | |
type pullup = LookupTable<Slide|Pullup, 'name'>['pullup'] // Pullup | |
type slide = LookupTable<Slide | Pullup, 'key'>['foo'] // Slide |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment