microsoft / microsoft/TypeScript
False-positive `unintentional comparison` error with closures
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
### 🔎 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_
Contributor guide
Research direction
Reproduce the closure example in the issue with TypeScript v7.0.2 and inspect the type-checker path that emits diagnostic 2367. Trace how control-flow narrowing is handled across updateN, then add a regression test covering both comparisons; done means the comparison after the closure call produces no false-positive warning while the example remains correctly checked.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100