输入:两种类型,输出:第三种类型,具有第一种类型的属性,重叠道具为第二种(如合并/赋值),适用于对象属性。
type X = {
a: string,
b: string
}
type Y = {
b: number,
d: number
}
// how to type get type Z that is "merge" of X & Y so that:
let z: Z
z.a => string
z.b => number
z.d => number
Z = X & Y
给出了z.a => string,z.b =>字符串|号码(我希望它只是数字)
答案 0 :(得分:1)
Typescript不支持此功能。你可以看看mixins:https://www.typescriptlang.org/docs/handbook/mixins.html。这是我找到的最接近你想要的东西,但它仍然无法解决你的问题:
class X {
a: number;
b: number;
}
class Y {
a: string;
d: number;
}
class Z implements X, Y {
a: any; // Note how I was need to make "any" type here to make it work
b: number;
d: number;
}
applyMixins(Z, [Y, X]); // Read provided link for documentation for this function
let x: X = { a: 2, b: 2 };
let y: Y = { a: '2', d: 2 };
let z: Z = { a: 'x', b: 2, d: 2 };