microsoft / microsoft/TypeScript
Add a compiler option to error on destructuring a string as an array
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
string destructuring
✅ 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
{
"compilerOptions": {
"allowDestructureStringAsArray": false
}
}
declare const source: string
const [item] = source
// ^^^^^ Error: destructuring string as array is not allowed
📃 Motivating Example
The codebase is everchanging. Someone might do a refactor that change an expression from returning an array of strings to returning a single string. TypeScript which was supposed to catch mistakes like this would fail.
-return Object.entries(myObject)
+return Object.keys()
.sort(([k1], [k2]) => k1.localeCompare(k2))
As you can see in the above example, the developer forgot to also update the .sort line, but TypeScript didn't alert it. This mistake is easy to catch in small code, but real code is much bigger and similar mistake would be harder to detect.
💻 Use Cases
- What do you want to use this for? Catching mistake during refactoring or code editing.
- What shortcomings exist with current approaches? There is no "current approaches".
- What workarounds are you using in the meantime? There is no workaround, eslint doesn't have such rule, and I doubt it cares about TypeScript's types.
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
L’issue ne nomme aucun fichier, test ni point d’entrée du compilateur. Commencez par suivre la définition des options du compilateur et la vérification de type du destructuring de tableaux. Le travail est terminé lorsqu’une nouvelle option peut refuser le destructuring d’une chaîne en tant que tableau, tout en conservant le comportement existant par défaut, avec une couverture pour l’exemple présenté.
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