Skip to content

Instantly share code, notes, and snippets.

@endymion1818
Created July 27, 2023 14:20
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 endymion1818/8433b13443e2136a533787cd8692ccb7 to your computer and use it in GitHub Desktop.
Save endymion1818/8433b13443e2136a533787cd8692ccb7 to your computer and use it in GitHub Desktop.
Pick only one
type RequireOnlyOne<T, Keys extends keyof T = keyof T> =
Pick<T, Exclude<keyof T, Keys>>
& {
[K in Keys]-?:
Required<Pick<T, K>>
& Partial<Record<Exclude<Keys, K>, undefined>>
}[Keys]
type Events = 'play' | 'pause' | 'ready' | 'seek' | 'end';
type ABC = {
on: (cb: RequireOnlyOne<Record<Events, () => void>>) => void
}
const abc: ABC = {
on: (cb) => {}
}
abc.on({ ready: () => console.log('yes') })
abc.on({ play: () => console.log('no'), pause: () => console.log('no') })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment