microsoft / microsoft/TypeScript
nested destructured tuple value is `string | undefined` when second tuple item is not accessed
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
Bug Report
When using noUncheckedIndexedAccess, the type inferred by my code is affected by whether I access the second item in a tuple or not.
🔎 Search Terms
tuple, undefined, type, omit, noUncheckedIndexedAccess, error, index, array, infer, nested
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
noUncheckedIndexedAccess - I was unable to test this on prior versions because
--noUncheckedIndexedAccessdidn't exist before those versions
⏯ Playground Link
Playground link with relevant code
💻 Code
declare const fn: <T extends string[]>(t: T) => [T, string[]];
const [[x]] = fn(['1']);
console.log(x.toLowerCase());
It seems that the inferred type for fn is:
const fn: <string[]>(t: string[]) => [string[], string[]]
Since the first item is an array rather than a tuple, items in that array have type string | undefined.
If I change the assignment to be:
const [[x], []] = fn(['1']);
It gets inferred as:
const fn: <[string]>(t: [string]) => [[string], string[]]
Since the first item is a tuple rather than an array, items in that tuple have type string.
🙁 Actual behavior
x has type string | undefined
🙂 Expected behavior
x has type string.
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
Commencez par le TypeScript Playground lié et reproduisez les deux formes de déstructuration avec noUncheckedIndexedAccess. Suivez les types inférés différents affichés dans le rapport ; le travail est terminé lorsque la première forme donne à x le type string tout en préservant le comportement attendu pour la seconde forme.
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