Skip to content

Incorrect error message when incorrectly implementing interface #23885

@hachiko-8ko

Description

@hachiko-8ko

TypeScript Version: 2.7.2

Search Terms: error message, language, incorrectly implements interface, not assignable to type

Code

// The following code contains errors, by design.
class x {
    xx: string[] = [];
    xxx: string = "";
}
class y {
    yy: string[] = [];
}
interface IFoo {
    Bar: x;
}
class Foo implements IFoo {
    Bar: y = new y();
}
class Test {
    fb: IFoo = new Foo();
}

Expected behavior:
The following error should be returned: Class "Foo" incorrectly implements class 'IFoo'. ... etc

Actual behavior:
in class Test, the following error is returned: Type 'Foo' is not assignable to type 'IFoo'. Types of property 'Bar' are incompatible. Type 'y' is not assignable to type 'x'.

This message might be literally correct, but if you're thinking object-oriented, it doesn't fit. Consider the code. This message states that Foo, which implements IFoo, is not assignable to IFoo, which is nonsensical. It is normal to set a interface type to a concrete class that implements the interface, and there should never be an error returned for doing so.

The actual problem here is that Foo is not correctly implementing the interface. Bar has the wrong type. In this specific example, you do get helpful error on Bar, but still no message stating that Foo incorrect implements IFoo. Also, in a more complex case, where each definition is in a different file, the message in Test is the only one you see.

I noticed this when I had just modified an interface to return a different type, and needed to hunt down the implementers to change them. The only errors returned were on places where in constructors of classes that referenced the type, which I found confusing and unhelpful. In C#, the moment an interface is changed to something incompatible, all the implementing classes raise errors, and I can follow the errors to the classes I need to fix.

It seems to me as if the only time you see the "incorrectly implements" message is when a property is missing. If the type exists but is incompatible (wrong return type, wrong arguments, etc), I think it should also count as incorrectly implementing it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions