Unreliably report for unused local variable in the editor
🔎 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,
- Open VSCode with the color theme set to any theme that supports semantic highlight, like the default dark+.
- 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).
- 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
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.