当 extractor
的类型为 handleEvent
时,我想重构以下内容以指定 TEvent
方法的 DefaultEvent
参数的默认实现。
而不是调用:
handleEvent<DefaultEvent>({value: "test"}, (e) => e.value);
如果可能,我希望以下内容是等效的:
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);