Last active
May 25, 2020 04:14
-
-
Save bmingles/ecbd4e3b835ef923aecd834f1542f02f to your computer and use it in GitHub Desktop.
Tagged Union Switch Expression
This file contains 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 Tagged<T extends string> = { | |
type: T | |
} | |
export function switchExp< | |
T extends Tagged<string>, | |
M extends { [P in T['type']]: T extends Tagged<P> ? (t: T) => any : never } | |
>(value: T, map: M): ReturnType<M[keyof M]> { | |
return map[value.type as keyof M](value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment