Skip to content

Unreliably report for unused local variable in the editorΒ #61403

Open
@jasonlyu123

Description

@jasonlyu123

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions