Created
July 27, 2023 14:20
-
-
Save endymion1818/8433b13443e2136a533787cd8692ccb7 to your computer and use it in GitHub Desktop.
Pick only one
This file contains hidden or 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
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