microsoft / microsoft/TypeScript

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

オープン
#61,106 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

### 🔍 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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

issue にはファイル、テスト、またはコンパイラのエントリポイントが記載されていません。まず、コンパイラオプションがどのように定義され、配列の分割代入がどのように型チェックされるかを追跡してください。既存の動作をデフォルトで維持しながら、新しいオプションによって文字列を配列として分割代入することを拒否でき、示された例のカバレッジが追加されていれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。