I'm trying to make a general purpose switch expression function that can take any discriminated union type (for simplicity using a type property as the discriminator) and a map of its discriminator values to callback functions and return the result of the appropriate callback.
e.g.
type One = {
type: 'one',
numeric: number
};
type Two = {
type: 'two',
text: string
};
type Union = One | Two;
const union: Union = ... // some appropriate assignment
// The function switchExp should be aware of what the map should
// look like based on the type of its first arg. The argument
// passed to each callback should be properly discriminated based
// on the key in the map.
let result: number | string = switchExp(union, {
one: u => u.numeric, // compiler should know that u is of type One
two: u => u.text // compiler should know that u is of type Two
});