microsoft / microsoft/TypeScript

False-positive `unintentional comparison` error with closures

未关闭
#64,314 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
Duplicate
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 Search Terms

unintentinal, comparison, 2367, overlap

### 🕗 Version & Regression Information

- typescript: `v7.0.2`
- node: `v24.14.1`
- pnpm: `v12.3.4`

### ⏯ Playground Link

_No response_

### 💻 Code

```ts
let n: "a" | "b" = "a";

const updateN = () => {
n = "b";
};

// This comparison appears to be unintentional because the types '"a"' and '"b"' have no overlap. [2367]
if (n === "b") {}

updateN();

// This comparison appears to be unintentional because the types '"a"' and '"b"' have no overlap. [2367]
if (n === "b") {}

n = "b";

// No error
if (n === "b") {}
```

### 🙁 Actual behavior

If a variable of type `"a" | "b"` is changed in a function with closure, the LSP fails to catch the change, and assumes that it was never changed.

### 🙂 Expected behavior

No error warning, since code works as expected

### Additional information about the issue

_No response_

贡献指南

打开贡献指南

调研方向

使用 TypeScript v7.0.2 重现 issue 中的 closure 示例,并检查发出 diagnostic 2367 的 type-checker 路径。跟踪跨越 updateN 时 control-flow narrowing 的处理方式,然后添加一个涵盖两个比较的回归测试;完成标准是 closure 调用后的比较不产生误报警告,同时该示例仍能被正确检查。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
活跃
描述清晰度
基本清楚
新手友好度
55/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。