microsoft / microsoft/TypeScript

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

Open
#61,106 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔍 Search Terms

string destructuring

### ✅ Viability Checklist

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

```json
{
"compilerOptions": {
"allowDestructureStringAsArray": false
}
}
```

```typescript
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.

```diff
-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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files, tests, or compiler entry points are named in the issue. Start by tracing how compiler options are defined and how array destructuring is type-checked. Done means a new option can reject destructuring a string as an array while preserving existing behavior by default, with coverage for the shown example.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.