Skip to content
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

Unreliably report for unused local variable in the editor #61403

Open
jasonlyu123 opened this issue Mar 12, 2025 Β· 1 comment
Open

Unreliably report for unused local variable in the editor #61403

jasonlyu123 opened this issue Mar 12, 2025 Β· 1 comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@jasonlyu123
Copy link
Contributor

πŸ”Ž Search Terms

declared but its value is never read
unused variable

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Common Misconceptions

⏯ Playground Link

https://www.typescriptlang.org/play/?noUnusedLocals=true#code/GYVwdgxgLglg9mABACxgCgJSIN4CgCQANgKZQ6JzDADOpA6jACZTKIC8iAjIgL7vmUa9JiwBciAAy8A3LgKDaUBs1YdOsvriA

πŸ’» Code

function hi() {
	let { offsetWidth = 1 } = { offsetWidth: 0 };

	offsetWidth = 1;
} 

πŸ™ Actual behavior

The problem is that the hint about the unused local variables is not reliably reported in the editor. I can't find a way to reproduce it in the playground. It is an error when noUnusedLocals=true, so it might cause more confusion than if it were just a hint.

It seems to be a race condition where Symbol.isReferenced is populated in another language feature before typecheck. The hint is then not being reported. Semantic highlight is the most reliable way I found to reproduce it. To reproduce this,

  1. Open VSCode with the color theme set to any theme that supports semantic highlight, like the default dark+.
  2. Paste the code to a ts file and see the hint not being reported like in the playground (if it is there, edit the file might make it disappear).
  3. Restart TSServer, and the hint is now reported. But editing the file hides the hint again.

πŸ™‚ Expected behavior

It was reliably reported no matter if other features checked the variable.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Interestingly it's only shown as unused when the TS server is restarted. When reloading the window or restarting VS Code it's not shown as unused. When editing the file it's shown as unused as well.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Mar 14, 2025
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants