Question

If in Typescript I have two interfaces which both have a member of same name, how can I securly implement both interfaces? 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?

Was it helpful?

Solution

TypeScript uses a structural type system, so there's absolutely no difference between IFace1 and IFace2. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top