Why does this Typescript subclass not inherit the interfaces implemented by its superclass?

Viewed 123

Consider the following typescript code:

interface MyInterface {
    foo?(x : string) : void;
}

class One implements MyInterface {
}

class Two extends One implements MyInterface {
    foo(x : number) : void {
    }
}

Compiling this throws an error as expected: Property 'foo' in type 'Two' is not assignable to the same property in base type 'MyInterface'.

When I remove the "implements MyInterface" in class Two, the error disappears. I would have expected that class Two inherits from class One the fact that it implements MyInterface, and so should still be compatible with it.

Coming from a Java background, I considered that I misunderstood the type system in TS, in particular whether a subclass instance "is-a" superclass instance. To verify, I added the method with a string parameter in class One, but this throws an error: Property 'foo' in type 'Two' is not assignable to the same property in base type 'One' -- leading me again to the conclusion that TS subclassses have an "is-a" relationship just as in Java. But that means that I still don't understand why class Two doesn't inherit from class One the fact that it implements MyInterface.

What did I get wrong?

0 Answers
Related