2

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?

1
  • 1
    TypeScript has no way to support this because the interface is a pure compile-time mechanism. The resulting JavaScript has no concept of the interface, the interface is erased. Given that, what would be your expected behavior to make this work? Commented Dec 16, 2013 at 20:09

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I thought that this would be the case. But how to deal with it then? It is a real-world problem. I'll create another question (as this one is perfectly answered) describing the problems and asking for how to work around them ;)
the concept behind it is also very nicely explained here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.