microsoft / microsoft/TypeScript
Unsafe Map overload spoils direct property access on parenthesized assignment expression
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
### 🔎 Search Terms
map overload any unknown unsafe weakmap set binary assignment lazy init
### 🕗 Version & Regression Information
- This is the behavior in every version I tried
### ⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250326#code/DYUwLgBAxghlAWIBcECyMAOAeAzmATgJYB2A5gDQQD2ARgFYhRgB8EAPhAK7EAmIAZiRA8A3ACgx-bk0JViEMCDwBGABQBrEAE8UeImQCUEAN5iI0OXgj4lnYJAC8EVbAQgIAfg9PiIAO5omKoGBgB0pOAa2gbi5gD0cebmAHoeZtbgnPjyNjh2YOIAvhJSxDJyCkpgAExROhB6JKRGpuauiJ7eEL4B6BjBsRbEVrn5EE7tIOGRmlox6QlJEKnpNmBZObb2RWJAA
### 💻 Code
```ts
let cache: Map | undefined;
function test1(key: string) {
const result = (cache ??= new Map()).get(key);
// ^? const result: any
return result;
}
function test2(key: string) {
cache ??= new Map();
const result = cache.get(key);
// ^? const result: object | undefined
return result;
}
```
### 🙁 Actual behavior
First `result` gets spoiled as `any`
### 🙂 Expected behavior
I'd expect both `result`s to be `object | undefined`
### Additional information about the issue
This bug reports an issue related to `Map` but, tbf, it's not unique to `Map` and a more general solution would have to be found to properly fix this. The problem is how `checkBinaryLikeExpressionWorker` create a union of left and right types here - without taking into consideration that the assignment target might already overlap with the right type. That union type gets reduced with `UnionReduction.Subtype` and `.get` gets read from it - instead of the assigned to reference on the left side.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
使用链接的 TypeScript Playground 重现 test1 和 test2 之间的差异,然后检查 checkBinaryLikeExpressionWorker;报告指出它负责形成赋值操作数的联合类型。跟踪该联合类型在解析 .get 之前是如何被缩减的。完成的标准是,带括号的赋值访问能够像单独的赋值形式一样推断出 object | undefined,并为报告中的示例添加回归覆盖。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100