假设我有两个接口(interface):
interface A {
a: string;
b: string;
x: string;
}
interface B {
a: string;
b: string;
}
当我有一个实现接口(interface) A
的对象并且我想删除属性 x
(并且该对象随后实现接口(interface) B
) -我如何告诉 TS 我想要做什么?仅在对象上执行 delete obj.x;
就会导致 TS 提示,因为接口(interface) A
需要 x
。
最佳答案
我就是这样做的。
interface A {
a: string;
b: string;
x: string;
}
interface B {
a: string;
b: string;
}
function convertAtoB(a: A) {
delete (a as any).x;
return a as B;
}
关于typescript - 如何通过删除属性将对象从接口(interface)A转换为接口(interface)B,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65224086/