microsoft / microsoft/TypeScript

Never-initialized variable type is not narrowed to undefined

Open
#61,496 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔎 Search Terms

narrow uninitialized, uninitialized undefined

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about `initialized`/`uninitialized`

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250326#code/MYewdgzgLgBAZiEMC8MAUUDuIBcNoBOAlmAOYCUeAbiEQCYoB8MA3gFAwwBEArhAKb4oxYFC4BuDjCkB6GTH4QANiSgBaOkQgBDAEZL+asPwAe6lcZgABKAE8ADouDF76xRagySRdXX7AlbQJtKCJwCCkDWHB+PEISUhgAHxgeMD84En46SSkiOHQYlGRULBByVilOORgAPQB+KpgagDlEeykAXzZutgQQNC4ucnEgA

### 💻 Code

```ts
const foo = (two: string): void => {
"use strict";

// eslint-disable-next-line @typescript-eslint/init-declarations
let one: string | undefined;

if (one === two) {
// ^? let one: string | undefined
// Noop
}
}

foo("");
```

### 🙁 Actual behavior

The type is not narrowed, which prevents typescript-eslint from flagging this comparison that always fails: [typescript-eslint playground](https://typescript-eslint.io/play/#ts=5.7.2&fileType=.tsx&code=MYewdgzgLgBAZiEMC8MAUUDuIBcNoBOAlmAOYCUeAbiEQCYoB8MA3gFAwwBEArhAKb4oxYFC4BuDjCkB6GTH4QANiSgBaOkQgBDAEZL%2BasPwAe6lcZgABKAE8ADouDF76xRagySRdXX7AlbQJtKCJwCCkDWHB%2BPEISUhgAHxgeMD84En46SSkiOHQYlGRULBByVilOORgAOUR7KQBfNha2BBA0Li5ycSA&eslintrc=N4KABGBECmAeAu0B2ATAzpAXGA2uCU0aANgJZLyYCGxxkANPhJAA7ECuA5uZgALwBPFkQDGAJ1It4AWiJkKAehp18AXUbMx7YkSxhQEAL4hDQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false)

### 🙂 Expected behavior

The `one === two` comparison should be flagged because it always returns false (there is no way `one` has any value other than `undefined`).

### Additional information about the issue

Also filed a related issue with ESLint: https://github.com/eslint/eslint/issues/19581

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior using the TypeScript Playground link and the `one === two` example, then trace how control-flow narrowing handles a never-initialized variable. Compare the result with the expected always-false comparison and use the linked ESLint issue for related context. Done means the comparison is recognized as impossible without changing unrelated narrowing behavior.

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
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.