microsoft / microsoft/TypeScript
Add switch to disable _var unused variable prevention
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
🔍 Search Terms
leading underscore unused variable
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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
tscto 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
📃 Motivating Example
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
- 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.
- 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.
- What workarounds are you using in the meantime?
There is no workaround.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Aucun fichier ni test n’est nommé. Commencez par retracer la manière dont TypeScript gère noUnusedParameters et supprime les informations sur les variables inutilisées pour les noms préfixés par un trait de soulignement, puis comparez les diagnostics du compilateur avec les rapports d’IDE/Tooling. Le travail est considéré comme terminé lorsqu’une option configurable conserve une compilation réussie tout en exposant ces variables à l’IDE comme étant inutilisées.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100