-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generic type no more correctly inferred since 3.7 (now inferred as any). #36226
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
This is the intended behavior. TypeScript has two candidates for
From the perspective of "how could Basically there's no way that class FieldEvent<T> {
constructor(readonly field: T) { }
}
class Field {
listen<T extends FieldEvent<Field>>(type: new (...args: Field[]) => T, listener: (event: T) => void): void {
// Plausible implementation
const j = new type(this);
listener(j);
}
}
let field = new Field();
field.listen(FieldEvent, (event) => event.field); |
Thanks @RyanCavanaugh for the code, which indeed helped me to fix the issue. However, playing with different versions of TS via playground:
Is it an expected/intended change? |
Yes; when TS infers more-specific types from lower-priority sites, unsoundness results. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@RyanCavanaugh, I wanted to apply your proposal to my code, but I cannot find the syntax working with overloads.
If we comment the overloads of It seems that TS only considers the 1st overload (not parsing all the overloads to find the 1st matching one). Notes:
|
TypeScript Version: 3.7.4, 3.8.0-dev.20200115
Search Terms: generic inferred any 3.7
Code
Expected behavior:
event
inferred asFieldEvent<Field>
(was the case up to TS 3.6).Actual behavior:
event
inferred asFieldEvent<any>
since TS 3.7. Still the case with 3.8.0-dev.20200115.Playground Link:
http://www.typescriptlang.org/play/index.html#code/MYGwhgzhAEBiCWBTEATAogN0QOwC4B4AVAPmgG8Aoaa6YAe2wlwCcBXYXO5gCmcTBQMQAT2gAzJKgBc0QgEpy0AL4UVFUJBgJkKclRoh4THEWiIAHrhwotk9Fjz5tqYsW65hAB0QzsiAO7Q3AB0oWDMAOYQMmDYwgDaALoKALykhAA00IbGfswy3IgOuDLy0GnQGHTwKHIyVTWKKmogiLjiduXQfoHOKNxyANwUEjrBOVbY3H2YOLhZhcWppEVzwaOoQxQA9NuyAMrQAMzBAOwyq3gyM8X4scLEWRB0ZsXrdjFxO3sAqp7QnAOx2CADYLsVrnZZo4+o9oM9XmsNihIToKEA
(Edit: playground link to official site, not beta).
Related Issues:
The text was updated successfully, but these errors were encountered: