简体   繁体   English

使用多个接口继承的打字稿名称冲突/冲突

[英]Typescript name collision/clash using multiple interface inheritance

If in Typescript I have two interfaces which both have a member of same name, how can I securly implement both interfaces? 如果在Typescript中我有两个接口都具有相同名称的成员,那么如何安全地实现这两个接口? Is this even possible? 这有可能吗?

Example: 例:

interface IFace1 {
    name: string;
}
interface IFace2 {
    name: string;
}

class SomeClass extends IFace1, IFace2 {
    // How to implement IFace1.name and IFace2.name ??
}

I know in C# this can be resolved and works because of C#'s type information at runtime, but what about Typescript? 我知道在C#中可以解决此问题,并且可以在运行时使用C#的类型信息,但是Typescript呢?

TypeScript uses a structural type system, so there's absolutely no difference between IFace1 and IFace2 . TypeScript使用结构类型系统,因此IFace1IFace2之间绝对没有区别。 You would implement them like this: 您可以这样实现它们:

class SomeClass implements IFace1, IFace2 {
    name = 'foo';
}

As vcsjones mentioned, because there is no runtime type information, there's not a plausible way where this could work on a nominal basis. 正如vcsjones所提到的,由于没有运行时类型信息,因此没有可行的方法可以名义上起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM