Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't show consecutive errors when incorrectly implementing an interface #46197

Closed
hediet opened this issue Oct 4, 2021 · 1 comment
Closed

Comments

@hediet
Copy link
Member

hediet commented Oct 4, 2021

Description

Example

When incorrectly but explicitly implementing an interface using implements, an error should only be shown for the incorrectly implementing class, but not at every location that casts the class to the interface (which is the current behavior).

interface IFoo {
    baz: string;
    bla: string;
}

class Foo implements IFoo { // Only show the error here
    baz = 'baz';
}

function test(foo: IFoo) { }

test(new Foo()); // Don't show the error here
test(new Foo()); // Don't show the error here
test(new Foo()); // Don't show the error here
test(new Foo()); // Don't show the error here
test(new Foo()); // Don't show the error here
test(new Foo()); // Don't show the error here
test(new Foo()); // Don't show the error here
test(new Foo()); // Don't show the error here
test(new Foo()); // Don't show the error here
test(new Foo()); // Don't show the error here

Motivation 1: implements as post-condition

When a function indicates that it returns a string, but the implementation returns a number instead, the error already is only shown for the function and not at every usage:

function test(): string {
    return 1; // Error is shown here
}

let x: string = test(); // Not here

As expected, test(): string indicates a publicly facing contract that test() is guaranteed to return a string. It is a local implementation detail that it does not.

Analogous, if Foo publicly states that it implements IFoo, every consumer should also be allowed to assume that Foo indeed implements IFoo.
Incorrect implementations are implementation details of the class and should not leak into usages.

Motivation 2: A Use-case

In the VS Code codebase, there are many interfaces that are implemented many times.
In some cases, the implementations are also used many times and very often passed to functions that expect the interface.

When changing/extending such an interface, hundreds of errors are reported: At every location that casts the class to the interface and at every class declaration that states it is implementing the interface.

This makes it very difficult to find all the locations where the interface is implemented incorrectly.

@nmain
Copy link

nmain commented Oct 4, 2021

Duplicate of #45880

My search terms were 'class implements'

@hediet hediet closed this as completed Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants