microsoft / microsoft/TypeScript
Potentially-`undefined` value not flagged when destructuring union of empty tuple and array
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
### 🔎 Search Terms
"tuple destructuring", "tuple narrowing"
### 🕗 Version & Regression Information
This bug appears to occur as far back as TS 3.3 up to the current nightly build, as tested in the Playground.
### ⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.8.2#code/C4TwDgpgBAggTnKBeWCCGIA8A7ArgWwCMI4A+KAHygG0BdSmgZ2DgEtsBzWgbgCheAJhADGAGzRxowgPbZmUCXABcqOH14y5wGmnopFfNADpg0gMot2HABQBKblAD0jqAFEAHpGHAIAlSThpOCNYb1w0URVsaSgAoJDAGXJ+TXlCZAUEagAGHl5CE3NLTjsHZzdPER8-WIR40OBwyJrA4KhAUHJ+XigoIA
### 💻 Code
```ts
type Arr = Array | [] | [string];
declare const arr: Arr;
const [a] = arr;
a.toString(); // Expected: error. Actual: no error. ❌
const b = arr[0];
b.toString(); // Expected: error. Actual: error. ✅
```
`.d.ts` from Playground:
```ts
type Arr = Array | [] | [string];
declare const arr: Arr;
declare const a: string | number | undefined;
declare const b: string | number | undefined;
```
### 🙁 Actual behavior
When destructuring a value whose type is an array, an empty tuple, and a non-empty tuple, the destructured value isn't flagged as possibly `undefined` which caused a runtime error. The value is correctly flagged as possibly `undefined` when accessing it by index.
### 🙂 Expected behavior
When destructuring a value whose type is an array, an empty tuple, and a non-empty tuple, the destructured value should be flagged as potentially being `undefined`.
### Additional information about the issue
This seems related to #55661 except, in this case, the union contains a mix of tuples and an array.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从链接的 TypeScript Playground 复现开始,并将元组解构推断出的类型与索引访问结果进行比较。查看相关 issue #55661 以了解背景。在这个数组与元组的混合 union 情况下,解构出的值被报告为可能是 undefined,同时不改变正确的索引访问行为,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 45/100