Last active
July 13, 2026 08:14
-
-
Save tijnjh/e8b3c575f9813988577c3382d21558f7 to your computer and use it in GitHub Desktop.
match.ts
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
| export function match<V extends PropertyKey, C>( | |
| value: V, | |
| cases: C & { | |
| [K in keyof C]: C[K] extends () => unknown ? unknown : never; | |
| } & ("_" extends keyof C | |
| ? Record<Exclude<keyof C, V | "_">, never> | |
| : Record<Exclude<keyof C, V>, never> & | |
| Record<Exclude<V, keyof C>, never>), | |
| ): C[keyof C] extends () => infer R ? R : never { | |
| // @ts-expect-error - implicit any to avoid casting | |
| return (cases[value] ?? cases._)(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.