I would like to refactor the following to specify a default implementation for the extractor parameter of the handleEvent method when TEvent is of type DefaultEvent.
Instead of calling:
handleEvent<DefaultEvent>({value: "test"}, (e) => e.value);
I would like the following to be equivalent if possible:
handleEvent<DefaultEvent>({value: "test"});
type DefaultEvent = {
value: string
}
type SpecificEvent = {
specificValue: string
}
function handleEvent<TEvent>(
e: TEvent,
extractor: (e: TEvent) => string
// default extractor implementation should be (e) => e.value when TEvent is of type DefaultEvent
) {
console.log(extractor(e));
}
handleEvent<DefaultEvent>({value: "test"}, (e) => e.value);
handleEvent<SpecificEvent>({specificValue: "test2"}, (e) => e.specificValue);