Skip to content

Instantly share code, notes, and snippets.

@antfu
Last active July 23, 2020 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antfu/67dd0cbddcd9ecec643d414c2b93be96 to your computer and use it in GitHub Desktop.
Save antfu/67dd0cbddcd9ecec643d414c2b93be96 to your computer and use it in GitHub Desktop.
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