-
Notifications
You must be signed in to change notification settings - Fork 12.8k
JSDoc @type does not attach to for loop variable declarations #43756
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
Also, for those who need a workaround, this works using TypeScript's JS "cast" feature, but I find it much harder to work with: for (const e of /** @type {NodeListOf<HTMLElement>} */ (document.body.querySelectorAll('.test'))) |
@Paril do you have an idea of how common this is? This is a non-trivial feature (though probably easy-ish) because Typescript explicitly disallows type annotations here. All the type checking would have to be added from scratch. |
In my usual code bases from Definitely Typed dependencies and our user tests + dependencies, I grepped Looking at this strictly as a new feature, I'm inclined to reference Closure and Typescript for precedent and say that allowing annotations here isn't that useful. |
I'm not sure about how common it is, I shouldn't have used the word "common pattern" in the initial post as I'm not actually certain of its usage but it was a pattern I saw as an accepted answer on how to do this sort of thing with JSDoc. It seems like it should be allowed; there's no other way to annotate a variable in a for/foreach statement otherwise, which seems like an odd missing feature. What's the reason for explicitly disallowing type annotations here? I'm curious as to why variable declarations in other contexts are okay to annotate but loop ones aren't, unless you move the declaration outside of the statement itself. I guess it just felt like it should "just work" because it immediately precedes the declaration. The two workarounds are much more verbose, and I can't see this being uncommon in the context of trying to use type checking inside of JS since querySelector and friends tend to prefer |
Actually, I think I found the TypeScript equivalent of this one, and I understand the reasoning for it being not accepted: Given how annotating this inline is essentially the JS version of this issue, I see the precedent for closing this one. However, the latter issue is still open for discussion; should this just be moved into that discussion perhaps? |
Sounds good to me! |
Also, I just realized that this syntax is fully valid in VSCode:
This isn't a huge thing, but, I think it's a good example of existing code that is similar to what I'm hoping for (although this is a parameter, not a local variable, but hey, same difference) |
Changed the labels to match #3500. |
For some reason this didn't work (or doesn't work anymore?), for those wondering, another workaround I found it to "cast" it beforehand by assigning it
|
Bug Report
From what I could tell, this is a common pattern with JSDoc used to properly document the type of a loop variable. It does not seem to work in VSCode.
🔎 Search Terms
jsdoc for loop type
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play?#code/GYewTgBAFA9AVHCABALgTwA4FMIG8ASAKgLIAyAogDZYC2WAdigL4RwwQDGI9AzihDhDAIAExAcArnUYA6AEYgRaGQEcJWMGgDKWahxTgAgpUpQA5DJRY+ZgJS28AKAguBMvmmoyRASx4ZKAEM0CABeCDN6biwzAG5HJiA
💻 Code
🙁 Actual behavior
e
is typed as Element🙂 Expected behavior
e
should be typed as HTMLElement, as I have overridden it via @type.As an example, if I were to change the code to this:
VSCode correctly picks up that I'm wishing to override
e
as an HTMLElement (and complains about missing interface implementations, but, that's not the issue here - in fact that's expected!)The text was updated successfully, but these errors were encountered: