microsoft / microsoft/TypeScript
Unions seem to sidestep `--noUncheckedIndexedAccess` and lose the possibility of undefined
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
noUncheckedIndexedAccess, union, undefined
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
--noUncheckedIndexedAccess
⏯ Playground Link
💻 Code
const nums: { [k: string]: number } = Math.random() < 0.5 ? { a: 1 } : { b: 2 };
const str = { a: "hello" }
// with --noUncheckedIndexedAccess on
const hmm = (Math.random() < 0.5 ? nums.a : str.a) // string | number | undefined
const wha = (Math.random() < 0.5 ? nums : str).a // string | number <-- 😕
🙁 Actual behavior
The type of wha is string | number, completely ignoring the possibility that it might be undefined, even though --noUncheckedIndexedAccess is enabled. Looks like unions of types with index signatures and known keys lose the --noUncheckedIndexedAccess behavior.
🙂 Expected behavior
wha should be of type string | number | undefined, just like hmm, since indexed access into a union should look like a union of indexed accesses.
Additional information about the issue
This is related to #50474. It’s also related to #47531, which had specifically to do with never[] and so the focus was on avoiding never[] as opposed to investigating what happened to the indexed access. ( https://github.com/microsoft/TypeScript/issues/47531#issuecomment-1229522810 )
Ran into this when looking at a Stack Overflow question
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 la reproduction liée dans TypeScript Playground en utilisant --noUncheckedIndexedAccess, et comparez les deux expressions d’accès indexé de l’issue. L’issue ne nomme aucun fichier ni test du dépôt ; suivez la manière dont l’accès indexé est résolu pour les unions, puis ajoutez une couverture montrant que la seconde expression inclut undefined et exécutez les tests pertinents du compilateur.
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
- 42/100