是否可以在子类/接口中删除继承的字段/方法? [英] Is it possible to remove an inherited field/method in a child class/interface?
本文介绍了是否可以在子类/接口中删除继承的字段/方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
遵循以下原则:
interface A {
a: number;
x: any;
}
interface B extends A {
b: number;
}
interface C {
a: number;
b: number;
}
因此,B
将等于C
(省略字段x
,但仍扩展为A
).是否有可能?如果可以,怎么办?
So the B
would be equal to C
(omitting field x
but still extending A
). Is it possible? If so, how?
推荐答案
不可能在TypeScript中删除接口的继承字段/方法.
It is impossible to remove an inherited field/method of an interface in TypeScript.
但是您可以通过重新设计接口来克服这一点:
But you can overcome this via interfaces reengineering:
-
提取基本界面
Extract base interface
interface BaseA {
a: number;
}
interface A extends Base A {
x: any;
}
interface B extends A {
b: number;
}
interface C extends BaseA {
b: number;
}
C
和B
都可以转换为BaseA
.
-
使用可选字段
Use optional field
interface A {
a: number;
x?: any;
}
interface B extends A {
b: number;
}
interface C extends A {
b: number;
}
我确定还有其他方法,具体取决于特定的任务上下文.
I'm sure there are other ways depending on the certain task context.
这篇关于是否可以在子类/接口中删除继承的字段/方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文