microsoft / microsoft/TypeScript
Unions seem to sidestep `--noUncheckedIndexedAccess` and lose the possibility of undefined
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
### 🔎 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
[Playground link](https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true#code/MYewdgzgLgBGCuBbCAuGBvGBtA1m6ATgJZgDmAumgogEYCmBMAvjALwwCyAhlABYB0BLmAAmIRAAoAlDAA8MAAz8ArDAD8GGFzQBGZjDSYaaAEzMA3AChQkWITabtMAES86AG3chnzS5YD0-jAA7kR8MAC0EWAgAKpgwG7AOHQiAJKidAAeqQCCwMB0EBAw4Nbg0DC8iIgOEtx8gsJikjLySqoa1BD8XAYwhL0ygQNQxGQwAD5wSPSM0-CZAGYkqeW2Ibx97PU8AkKi4tJyiirqM8j9hFK9MCOEJKRTF3N+QA)
### 💻 Code
```ts
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](https://stackoverflow.com/q/79451704/2887218)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione collegata in TypeScript Playground usando --noUncheckedIndexedAccess e confronta le due espressioni di accesso indicizzato nell’issue. L’issue non indica alcun file o test del repository; ricostruisci come viene risolto l’accesso indicizzato per le unioni, quindi aggiungi una copertura che mostri che la seconda espressione include undefined ed esegui i test pertinenti del compilatore.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100