microsoft / microsoft/TypeScript
'in' does not remove undefined from indexed value (with noUncheckedIndexedAccess)
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
### 🔎 Search Terms
in operator, noUncheckedIndexedAccess
### 🕗 Version & Regression Information
Tested 5.8.2 and 5.9.0-dev.20250310
### ⏯ Playground Link
https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&ts=5.8.2#code/MYewdgzgLgBAhgJwQLhmArgWwEYFMEDaAujALwwECMANDAEy0DMRA3AFCiSwCWAJgB6oMOfGXrtuAMxgAKPvxjcw8JAEoYAbzYwdMTtBiC0WPAjGJC81tt36QAG1wA6eyADmM-qvYBfNkA
### 💻 Code
With an array:
```ts
const arr: number[] = [1, 2, 3];
const idx: number = 2;
if (idx in arr) {
const x: number = arr[idx];
console.log(x);
}
```
With a map/object:
```ts
const map: Record = { a: 1 };
const key: string = "a";
if (key in map) {
const x: number = map[key];
console.log(x);
}
```
### 🙁 Actual behavior
```
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
```
### 🙂 Expected behavior
Expected 'in' operator to guarantee that the result of indexing is not `undefined` in this branch.
### Additional information about the issue
_No response_
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
Start with the linked TypeScript Playground reproduction using noUncheckedIndexedAccess and both the array and Record examples. Trace how the type checker narrows indexed access inside the `in` branches. Done means both examples allow assigning the indexed values to number without weakening the option’s existing behavior.
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100