microsoft / microsoft/TypeScript
Unions seem to sidestep `--noUncheckedIndexedAccess` and lose the possibility of undefined
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
### 🔎 Search Terms
noUncheckedIndexedAccess, union, undefined
### 🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about `--noUncheckedIndexedAccess`
### ⏯ Playground Link
[Playground link](https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true#code/MYewdgzgLgBGCuBbCAuGBvGBtA1m6ATgJZgDmAumgogEYCmBMAvjALwwCyAhlABYB0BLmAAmIRAAoAlDAA8MAAz8ArDAD8GGFzQBGZjDSYaaAEzMA3AChQkWITabtMAES86AG3chnzS5YD0-jAA7kR8MAC0EWAgAKpgwG7AOHQiAJKidAAeqQCCwMB0EBAw4Nbg0DC8iIgOEtx8gsJikjLySqoa1BD8XAYwhL0ygQNQxGQwAD5wSPSM0-CZAGYkqeW2Ibx97PU8AkKi4tJyiirqM8j9hFK9MCOEJKRTF3N+QA)
### 💻 Code
```ts
const nums: { [k: string]: number } = Math.random() < 0.5 ? { a: 1 } : { b: 2 };
const str = { a: "hello" }
// with --noUncheckedIndexedAccess on
const hmm = (Math.random() < 0.5 ? nums.a : str.a) // string | number | undefined
const wha = (Math.random() < 0.5 ? nums : str).a // string | number <-- 😕
```
### 🙁 Actual behavior
The type of `wha` is `string | number`, completely ignoring the possibility that it might be `undefined`, even though `--noUncheckedIndexedAccess` is enabled. Looks like unions of types with index signatures and known keys lose the `--noUncheckedIndexedAccess` behavior.
### 🙂 Expected behavior
`wha` should be of type `string | number | undefined`, just like `hmm`, since indexed access into a union should look like a union of indexed accesses.
### Additional information about the issue
This is related to #50474. It’s also related to #47531, which had specifically to do with `never[]` and so the focus was on avoiding `never[]` as opposed to investigating what happened to the indexed access. ( https://github.com/microsoft/TypeScript/issues/47531#issuecomment-1229522810 )
Ran into this when looking at [a Stack Overflow question](https://stackoverflow.com/q/79451704/2887218)
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从使用 --noUncheckedIndexedAccess 的链接 TypeScript Playground 复现开始,并比较 issue 中的两个索引访问表达式。issue 没有指出任何仓库文件或测试;追踪联合类型的索引访问是如何解析的,然后添加覆盖,表明第二个表达式包含 undefined,并运行相关的编译器测试。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100