-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Compiler reports instance is "not assignable to X" even if class is declared to implement X #45880
Comments
I can agree that this is not the best error reporting experience if you were designing it from a blank slate, but this is the only thing that makes sense given our structural type system. The whole idea of a structural type system is that you don’t get to be a |
It does feel similar to the incorrectly-returning function example, but the return type annotation on a function is a much stronger source of intent than an |
Devil’s advocate: If a type breaks the contract of its |
[cries in #32082 and assortment of linked issues] |
Isn't this still the same as in the return type analogy? The function body can return values with different types than the declared return type, as long as the values remain assignable to the return type. |
No: interface I {
p: string;
}
class C implements I {
p: "hello" = "hello";
}
new C().p; // "hello"
function f(): string {
return "hello" as const;
}
f(); // string |
@andrewbranch: Ah, that's what you mean. Makes sense. It still feels like the desired behavior is for TS to only error at the class declaration site and not at the use site, and it doesn't seem like that's actually incompatible with a structural type system. For example, one way to see it: the type of interface I {
p: string;
}
class C implements I {
p: "hello" = "hello";
}
function f(i: I) {
i.p = "bye";
}
f(new C()); // loose variance sinks ships :-) |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
🔎 Search Terms
error ignores declared interface
🕗 Version & Regression Information
Tested in the Playground and the issue occurs in 3.3.3 in the playground with 4.4.2 and nightly (4.5.0-dev20210910).
⏯ Playground Link
Link link with relevant code
💻 Code
🙁 Actual behavior
The TypeScript compiler reports an error in the
getLogger()
function.This is not a major problem, since fixing the root cause also gets rid of the error. But it's not ideal because the second error is mostly noise.
This feels analogous to this situation (playground link):
🙂 Expected behavior
I would expect there to be no error reported in the
getLogger()
function.The text was updated successfully, but these errors were encountered: