I have a union type:
type Msg = { type: 'banana', value: number[] } | { type: 'apple' };
now i would like to create a function that creates such a Fruit type, with two arguments and takes a generic for the Msg:
const createMsg = <M extends { type: string }>(type: M["type"], value?: M["value"]) => ({ type, value });
The value argument of the function should be linked to the type of value of the union type, and if it's not defined, the param should be optional. I would like to be able to call it in two ways:
createMsg('banana', [1, 2, 3]);
createMsg('apple');
All my current solutions so far forced me to provide a second argument undefined for the 'apple' union-type.