如果在 Typescript 中我有两个接口(interface),它们都有同名的成员,我如何安全地实现这两个接口(interface)?这可能吗?
示例:
interface IFace1 {
name: string;
}
interface IFace2 {
name: string;
}
class SomeClass extends IFace1, IFace2 {
// How to implement IFace1.name and IFace2.name ??
}
我知道在 C# 中,由于 C# 在运行时的类型信息,这个问题可以得到解决并且有效,但是 Typescript 呢?
最佳答案
TypeScript 使用结构类型系统,因此 IFace1
和 IFace2
之间绝对没有区别。您可以像这样实现它们:
class SomeClass implements IFace1, IFace2 {
name = 'foo';
}
正如 vcsjones 提到的,由于没有运行时类型信息,因此没有一种合理的方式可以在名义上发挥作用。
关于c# - 使用多接口(interface)继承的 typescript 名称冲突/冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20619894/