Last active
May 26, 2026 16:59
-
-
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< | |
| TValue extends PropertyKey, | |
| TCases extends Record<TValue, () => unknown>, | |
| >(value: TValue, cases: TCases): ReturnType<TCases[TValue]>; | |
| export function match< | |
| TValue extends PropertyKey, | |
| TCases extends Partial<Record<TValue, () => unknown>>, | |
| >( | |
| value: TValue, | |
| cases: TCases & { _: () => unknown }, | |
| ): ReturnType<NonNullable<TCases[keyof TCases]>>; | |
| // @ts-expect-error - implicit any to allow for overloads | |
| export function match(value, cases) { | |
| return (cases[value] ?? cases._)(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment