microsoft / microsoft/TypeScript

Add switch to disable _var unused variable prevention

Offen
#63,143 10 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

🔍 Search Terms

leading underscore unused variable

✅ Viability Checklist
⭐ Suggestion

I always knew that _prefixed variables are ignored, but only recently I learned that it not only makes the IDE ignore unused variables, but it prevents the TS engine itself from even reporting whether a variable is used/unused to the IDE. I personally believe that a language engine should not be concerned by this and instead report everything raw and let the IDE and tooling handle it however the user wants.

What I want is to allow TS engine to compile without errors (if a variable is unused), but still report this information to the IDE and tooling.

Therefore, to not break existing codebases, I propose a new tsconfig flag to disable this implicit behavior.

With this new flag enabled, the TS engine will report whether a variable is used / unused to the IDE even if the variable name is prefixed with _.

This is especially useful in situations when one tries to avoid shadowing variable names, which could unknowingly lead to triggering this implicit behavior. And I am pretty sure that there is a myriad of other use cases where the loss of used/unused information is unwanted and unexpected.

const table = ...
// later
function replaceTable(_table) { // <- wrong, this variable is unused, but the IDE can't report this because of TS
  table = makeTable()
}

This setting does not affect noUnusedParameters, instead, they will work in tandem.

// tsconfig
noUnusedParameters: false,
reportUnusedUnderscoreVariables: true // <- just an adhoc placeholder

This will

  • allow tsc to compile even if there are unused variables
  • report unused variables prefixed with _ as unused to the IDE
  • show squiggles "unused variable" in the IDE
  • allow user to configure their tooling (eslint, etc.) to use regular expressions to selectively ignore their chosen variation and be not locked to using _name

related: https://stackoverflow.com/questions/79884683/what-is-causing-vs-code-to-ignore-prefixed-arguments-like-arg-ts6133

📃 Motivating Example

https://stackoverflow.com/questions/79884683/what-is-causing-vs-code-to-ignore-prefixed-arguments-like-arg-ts6133

I wrote a function that uses a column variable, but there was a column defined in the scope, so I renamed it to _column. Later, I encountered a bug and while investigating, I found that the variable was not used, but this fact was unknown to the IDE.

I have some unused parameters in other places, completely intentional, so I already disabled noUnusedParameters in tsconfig, because I don't use underscore _name to denote that a variable is unused. It's noise as I have IDE for that already.

💻 Use Cases
  1. What do you want to use this for?

I want to use variable names with underscore prefix to avoid shadowing existing variables, but I still want the IDE to report these variables to me for my consideration without breaking the build.

  1. What shortcomings exist with current approaches?

Regardless of the noUnusedParameters setting, the IDE will not receive information whether a variable is used/unused if it is prefixed with underscore.

  1. What workarounds are you using in the meantime?

There is no workaround.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Es werden keine Dateien oder Tests genannt. Beginne damit nachzuverfolgen, wie TypeScript noUnusedParameters behandelt und Informationen über ungenutzte Variablen für Namen mit vorangestelltem Unterstrich unterdrückt; vergleiche anschließend Compilerdiagnosen mit der Meldung durch IDE/Tooling. Als erledigt gilt die Aufgabe, wenn eine konfigurierbare Option eine erfolgreiche Kompilierung beibehält und diese Variablen zugleich der IDE als ungenutzt offenlegt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.