microsoft / microsoft/TypeScript
Narrow object property indexed by another object's property of unique symbol type (like `Symbol.iterator`)
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
### 🔍 Search Terms
type guard, narrowing, control flow analysis, property, index, known type, symbol type, bracket notation, unique symbol, known symbol, `Symbol.iterator`, #10530
### ✅ 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
This is yet another version of #10530 that doesn't seem to be covered by cases in #56389 or #61176. It *might* even be covered by #62247 and/or #62314 but I'm not sure. TypeScript already narrows by bracket notation when the key is a unique symbol variable:
```ts
declare const s: unique symbol;
declare const x: {}
if ((s in x) && (typeof x[s] === "string")) { x[s].toUpperCase(); }; // okay
```
but this no longer works if the key is a unique symbol property of another object:
```ts
declare const obj: { readonly s: unique symbol }
declare const x: {}
if ((obj.s in x) && (typeof x[obj.s] === "string")) { x[obj.s].toUpperCase(); } // error
```
The suggestion here is to make the latter work like the former.
### 📃 Motivating Example
See https://stackoverflow.com/questions/79877994/why-does-my-test-for-an-iterableiterator-fail
Consider
```ts
function f(x: object) {
if ((Symbol.iterator in x) && (typeof x[Symbol.iterator] === "function")) {
x[Symbol.iterator].length; // error
//~~~~~~~~~~~~~~~~ <-- Object is of type 'unknown'.(2571)
}
}
```
This just looks like a somewhat unfortunate gap in type guarding, where TS knows `Symbol.iterator` is a unique symbol but won't actually do the narrowing unless you put that unique symbol in its own variable directly:
```ts
function f(x: object) {
const symbolIterator: typeof Symbol.iterator = Symbol.iterator;
if ((symbolIterator in x) && (typeof x[symbolIterator] === "function")) {
x[symbolIterator].length; // okay
}
}
```
[Playground link](https://www.typescriptlang.org/play/?target=99#code/CYUwxgNghgTiAEYD2A7AzgF3mgXPArigJYCO+CaAngLYBGSEA3AFCiSwLLpZK0BWeAN7w4UYKgiVseQqXLYa9CPAC+rcNDiJUmeAA8haogDN4ACjNp4RFPoCU8AGSPzGSgAcQSU3oDaaAF14AF5Q+AAiTBgbAHNwuwdhP0CAOgwkAFV3TxgAYSg0EDM7RlVSgHpy+CQAayhKZhNzM14+FKsbeycXMzdPb31fVvag0OCIqNj4xMHh1PSsnPzC4tKVeEr4EBgYJBhmZmNCMAwiVHhjMwNq-nAMROZ4J+tTCwBlRQYUogxtqHSYNZbHoHM5XB4vD5fB86F8fn8AaMwuEjigTmcUNN4IJHs88X4YUpvr8YP89gEUhAQCgYhgABYVKrbXb7PF4yoAPy53J5vI58AAPABaIXwADytxO1isAz6CAA5IQaigkAB3FDylJmABMAFYAOwARjsuKealN2m4ClhEAAkiSyTA8HKBoS4Q6ASF4G6IMSEXsWHimhYqDb7f7AZ0Qd1wf0oaGlOHSYiQsjUejUFicWznslPnaPeTKdTaQyNlVavULWpzUA)
### 💻 Use Cases
1. What do you want to use this for? To allow people to type guard on `Symbol.XXX` properties
2. What shortcomings exist with current approaches? People apparently don't like to copy things to new variables just for narrowing.
3. What workarounds are you using in the meantime? Copy things to new variables. The obvious approach is the old standby, where we replace `if (f(obj[k])) g(obj[k])` with `const objK = obj[k]; if (f(objK)) g(objK)`
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、ブラケット記法に対する TypeScript の制御フロー解析と型ガード処理を読み、動作する unique-symbol 変数の例と、失敗する obj.s および Symbol.iterator のケースを比較します。関連する issue #10530、#56389、#61176、#62247、#62314 を確認し、その後、変更が完了したときにプロパティアクセスによる絞り込みが一貫して行われることを、提供された Playground の例で検証します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100