Skip to content

Instantly share code, notes, and snippets.

@bmingles
Last active May 25, 2020 04:14
Show Gist options
  • Save bmingles/ecbd4e3b835ef923aecd834f1542f02f to your computer and use it in GitHub Desktop.
Save bmingles/ecbd4e3b835ef923aecd834f1542f02f to your computer and use it in GitHub Desktop.
Tagged Union Switch Expression
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