Skip to content

Add a compiler option to error on destructuring a string as an arrayΒ #61106

@KSXGitHub

Description

@KSXGitHub

πŸ” Search Terms

string destructuring

βœ… Viability Checklist

⭐ 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

  1. What do you want to use this for? Catching mistake during refactoring or code editing.
  2. What shortcomings exist with current approaches? There is no "current approaches".
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions