microsoft / microsoft/TypeScript

JSDoc @type does not attach to for loop variable declarations

Open
#43,756 10 comments 8 reactions 1 assignee View on GitHub

@sandersn is already working on this.

Since Apr 21, 2021.

Awaiting More Feedback Needs Investigation Rescheduled Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about JSDoc.
⏯ Playground Link

https://www.typescriptlang.org/play?#code/GYewTgBAFA9AVHCABALgTwA4FMIG8ASAKgLIAyAogDZYC2WAdigL4RwwQDGI9AzihDhDAIAExAcArnUYA6AEYgRaGQEcJWMGgDKWahxTgAgpUpQA5DJRY+ZgJS28AKAguBMvmmoyRASx4ZKAEM0CABeCDN6biwzAG5HJiA

💻 Code
for (/** @type {HTMLElement} */ const e of document.body.querySelectorAll('.test')) {
    e.style.display = 'none';
}
🙁 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:

/** @type {HTMLElement} */ const e = document.body.querySelectorAll('.test');

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!)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.