microsoft / microsoft/TypeScript
Narrow object property indexed by another object's property of unique symbol type (like `Symbol.iterator`)
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 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 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先阅读 TypeScript 针对方括号表示法的控制流分析和类型保护处理,并比较可正常工作的 unique-symbol 变量示例与失败的 obj.s 和 Symbol.iterator 情况。查看相关 issue #10530、#56389、#61176、#62247 和 #62314,然后使用提供的 Playground 示例验证更改完成后属性访问是否能一致地进行缩窄。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100