microsoft / microsoft/TypeScript
Never-initialized variable type is not narrowed to undefined
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
### 🔎 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
Guía de contribución
Línea de trabajo
Reproduce el comportamiento usando el enlace de TypeScript Playground y el ejemplo `one === two`; después, rastrea cómo el estrechamiento del flujo de control gestiona una variable que nunca se ha inicializado. Compara el resultado con la comparación esperada que siempre es falsa y usa el issue enlazado de ESLint como contexto relacionado. Se considera terminado cuando la comparación se reconoce como imposible sin cambiar el comportamiento de estrechamiento no relacionado.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 45/100