type type_a = {
id:number
OnThink?: (info: type_a)=>number
}
type type_b = type_a & {
width:number
OnThink?: (info: type_b)=>number
}
type type_c = type_a & {
delay:number
OnThink?: (info: type_c)=>number
}
function async_d<T extends type_a = type_a>(info:T){
let count = info.OnThink?.(info)
// ...
}
// 使用时
let newInfo:type_b = {
id:2,
OnThink:info=>{ return info.id + info.width }
}
async_d(newInfo)
希望
asyn_c
函数接受所有的type_a
的变型或者拓展类型。- 该拓展类型中的成员函数
OnThink
含有形参的类型为拓展类型。
是否能在type_a
中做好对Onthink
的类型定义,而不是在定义type_b
和type_c
的时候重新定义?
来看 TypeScript Playground