如何合并既有调用签名又有属性的类型,而不诉诸交集&
?交叉点&
创建的类型如Foo & { a: string } & { ()=>void } & Bar
所示,可能难以理解和抽象.当我们想要的是{()=>void, a: string, b: string, c: number }
时,它可以更容易解析.
type Merge<T1, T2> = {
[k in keyof T1 | keyof T2]: k extends keyof T1 ? T1[k] : k extends keyof T2 ? T2[k] : never;
}
type Foo = {
(a: string): number;
(a: number): number;
foo1: string
}
type Bar = {
bar1: number
}
type FooBar = Merge<Foo, Bar> // Merge excluding the call signatures - how does one include the call signatures?
/* end type should be:
type FooBar = {
(a: string): number;
(a: number): number;
foo1: string
bar1: number
}
and not be an intersection `Foo & Bar`
*/