microsoft / microsoft/TypeScript

Potentially-`undefined` value not flagged when destructuring union of empty tuple and array

Ouverte
#61,424 4 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Domain: check: Control Flow Help Wanted
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

### 🔎 Search Terms

"tuple destructuring", "tuple narrowing"

### 🕗 Version & Regression Information

This bug appears to occur as far back as TS 3.3 up to the current nightly build, as tested in the Playground.

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.2#code/C4TwDgpgBAggTnKBeWCCGIA8A7ArgWwCMI4A+KAHygG0BdSmgZ2DgEtsBzWgbgCheAJhADGAGzRxowgPbZmUCXABcqOH14y5wGmnopFfNADpg0gMot2HABQBKblAD0jqAFEAHpGHAIAlSThpOCNYb1w0URVsaSgAoJDAGXJ+TXlCZAUEagAGHl5CE3NLTjsHZzdPER8-WIR40OBwyJrA4KhAUHJ+XigoIA

### 💻 Code

```ts
type Arr = Array | [] | [string];

declare const arr: Arr;

const [a] = arr;
a.toString(); // Expected: error. Actual: no error. ❌

const b = arr[0];
b.toString(); // Expected: error. Actual: error. ✅
```

`.d.ts` from Playground:
```ts
type Arr = Array | [] | [string];
declare const arr: Arr;
declare const a: string | number | undefined;
declare const b: string | number | undefined;
```

### 🙁 Actual behavior

When destructuring a value whose type is an array, an empty tuple, and a non-empty tuple, the destructured value isn't flagged as possibly `undefined` which caused a runtime error. The value is correctly flagged as possibly `undefined` when accessing it by index.

### 🙂 Expected behavior

When destructuring a value whose type is an array, an empty tuple, and a non-empty tuple, the destructured value should be flagged as potentially being `undefined`.

### Additional information about the issue

This seems related to #55661 except, in this case, the union contains a mix of tuples and an array.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par la reproduction liée dans TypeScript Playground et comparez le type inféré pour la déstructuration de tuple avec le résultat de l'accès indexé. Consultez l'issue associée #55661 pour le contexte. Le travail est terminé lorsque la valeur déstructurée est signalée comme potentiellement undefined dans ce cas d'union entre un tableau et un tuple, sans modifier le comportement correct de l'accès indexé.

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é
Clairement spécifiée
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.