-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Unexpected behavior when static methods are used in an array #41977
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
Comments
Excellent repro btw. This dates back to at least 3.3.3. A fix might be difficult... |
Yeah this is longstanding and a problem with subtyping being non-directional in the presence of bivariant functions. A similar repro elucidates the problem. Observe the .d.ts output of this program: interface S {
f(a: number | string): void;
}
declare const S: S;
function g(a: number): void { }
// No-op lines
g;
S.f;
const arr = [g, S.f]; In this example, we give |
Hypothetical fix from talking to @ahejlsberg and @weswigham is to remove method bivariance in the subtype relationship but keep it in the assignability relationship. This would definitely fix this issue, the question is simply a matter of what side effects it would have. |
I've been experimenting a little, and it looks like we can fix this issue by making I'm convinced that changing the strict subtype relation to always check function types strictly (regardless of the I will put up a PR with my suggested change. |
TypeScript Version: 4.2.0-dev.20201211 (also tested with 4.2.0)
Search Terms: static method(s)
Code
Expected behavior:
Consistent type checking result (an error) for the
output
const with and withouttype G = typeof g
.Actual behavior:
If the
type G = typeof g
is present, the error is thrown (as expected), otherwise there is no error thrown.Playground Link:
Playground link to reproduce the error.
// cc @alxhub @rkirov
The text was updated successfully, but these errors were encountered: