-
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
Property assignment in constructor with JSDoc @param wrongly reports type as any #61005
Comments
It's not incorrect, within the constructor I admit this is surprising though when you don't know about those auto/evolving types, see #54414 |
Understood, thanks! But the terms “auto type” and “evolving type” are not found in the typescriptlang.org search, so this is an internal detail (or at least the terminology is). So this does sound like it's “correct” only in an overly narrow sense (which is probably what led you to phrase it as “not incorrect” :) ) ... narrow meaning:
When the constructor does assign the property in different places with different types (like in your playground example), a more consistent and useful behavior would be to show the type as:
Note that I originally reported this in https://github.com/microsoft/vscode/, because this is a matter of how type information is expressed to an end user in VS Code (not of how code is analyzed locally in the constructor) ... so it can make use of information known at the end of type analysis (i.e. it can know the result of the type evolution, and express that to the user). |
... oh, right, I guess this is pretty much what is proposed in #54414 :) ... so maybe this is just a duplicate. |
Well, except #54414 focuses on arrays. The case of JS class properties whose existence and type is inferred from assignments in the constructor is maybe more tractable, because the type is presumably auto/evolving only within the constructor body, right? |
That’s the same DX problem really. The „scope” in which this is allowed to evolve doesn’t quite matter if we think about the implementation complexities involved with improving this |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Type: Bug
In this code:
the type of property
bar
is inferred from the parameter type via the constructor assignment, as verified by:(property) Foo.bar: number
when I hover onfoo.bar
in the last linetsc --noImplicitAny
making no complaintsbut VS Code shows
(property) Foo.bar: any
when I hover onthis.bar
in the constructor.I think that's simply a bug — at least I can't think of a reason it ought to be that way, and it has misled me into spending time troubleshooting inference or explicitly declaring the type.
Seen originally in latest stable VS Code and now also in VS Code Insiders with no extensions.
VS Code version: Code - Insiders 1.97.0-insider (Universal) (3d0aeb47a2ecfde9ff5141470b30c36d41c321d9, 2025-01-20T05:04:25.114Z)
OS version: Darwin arm64 24.2.0
Modes:
System Info
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off
A/B Experiments
The text was updated successfully, but these errors were encountered: