-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Class property type inference failure when destructuring return value of generic function #51203
Comments
For context:
Looks like a design limitation. Essentially tl;dr: the type of a property is both the input and output of a generic instantiation, the compiler can't resolve the cycle and so falls back to |
As an additional note: If you're wondering why the compiler thinks let x: Promise<number> = new Promise(resolve => resolve("foo")); // error on resolve call |
Well, I'm not familiar enough with the granularities of the type checker logic in order to refute what you're saying, but the error is still generated in the case when a default generic type argument is defined for class Foo {
constructor() {
[this.x, this.y] = init();
}
x; // Still fails
y; // Still fails
}
function init<T = string>() {
return [0, ""];
} If I'm understanding what you're saying correctly, then I would imagine that such a work-around should at least dodge the issue. But the error persists. Also worth noting that if the generic argument is provided at the call site rather than in the signature, everything works as expected: class Foo {
constructor() {
[this.x, this.y] = init<string>();
}
x; // number
y; // string
}
function init<T>() {
return [0, ""];
} |
The default is only used when TS can’t successfully infer a type for the generic. It’s still going to try to infer it through the usual means, and thus hit the aforementioned circularity (which will indeed end with That providing the type argument at the call site dodges the error is fully expected - it bypasses generic inference (there’s no reason to prefer an inference over a type provided explicitly). |
Basically, any case where type inference X depends on type inference Y and vice versa will likely be detected as circular, even if there’s an alternate way to infer X that avoids the circularity, because inference is only done in a single pass. TS doesn’t do full unification (see #30134). |
Short version: TS tries to infer both Fix would be to do type inference in multiple rounds (allowing failed inferences to be retried using information from the previous round), but responses to similar issues in the past have suggested it’s a performance issue. |
This is a true circularity, as outlined above. |
Well, technically, it’s a finite spiral that’s just being viewed from above— |
This issue has been marked as 'Not a Defect' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
🔎 Search Terms
Class Property
Destructuring
Type Inference
Generic
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The
x
andy
properties are typed asany
.🙂 Expected behavior
The
x
andy
properties should be typed asnumber
andstring
respectively.Further investigation
If the return value of the function is captured in a local variable, and then destructured from there, everything works as expected:
Or, if the generic argument is omitted, everything works as expected:
Making use the generic type argument
T
does not appear to resolve the issue:The text was updated successfully, but these errors were encountered: