microsoft / microsoft/TypeScript
'in' does not remove undefined from indexed value (with noUncheckedIndexedAccess)
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
### 🔎 Search Terms
in operator, noUncheckedIndexedAccess
### 🕗 Version & Regression Information
Tested 5.8.2 and 5.9.0-dev.20250310
### ⏯ Playground Link
https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&ts=5.8.2#code/MYewdgzgLgBAhgJwQLhmArgWwEYFMEDaAujALwwECMANDAEy0DMRA3AFCiSwCWAJgB6oMOfGXrtuAMxgAKPvxjcw8JAEoYAbzYwdMTtBiC0WPAjGJC81tt36QAG1wA6eyADmM-qvYBfNkA
### 💻 Code
With an array:
```ts
const arr: number[] = [1, 2, 3];
const idx: number = 2;
if (idx in arr) {
const x: number = arr[idx];
console.log(x);
}
```
With a map/object:
```ts
const map: Record = { a: 1 };
const key: string = "a";
if (key in map) {
const x: number = map[key];
console.log(x);
}
```
### 🙁 Actual behavior
```
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
```
### 🙂 Expected behavior
Expected 'in' operator to guarantee that the result of indexing is not `undefined` in this branch.
### Additional information about the issue
_No response_
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Start with the linked TypeScript Playground reproduction using noUncheckedIndexedAccess and both the array and Record examples. Trace how the type checker narrows indexed access inside the `in` branches. Done means both examples allow assigning the indexed values to number without weakening the option’s existing behavior.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100