Is it possible to configure TypeScript to allow omitting the trailing parameter of a generic function if the trailing parameter's type is undefined (but not if it isn't)?
Example:
type Events = {
readonly first: { readonly foo: string};
readonly second: undefined;
}
const sendEvent = <Name extends keyof Events>(name: Name, payload: Events[Name]) => {
console.log(`SEND EVENT "${name}"`, name, payload);
}
sendEvent('first', { foo: 'bar'}); // <-- OK
sendEvent('second', undefined); // <-- OK
sendEvent('first'); // <-- Error as expected, Events["first"] isn't undefined,
// that's good
sendEvent('second'); // <-- Error: 'Expected 2 arguments, but got 1.'
// would like to not have an error here, since
// Events["second"] is undefined