Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unions seem to sidestep --noUncheckedIndexedAccess and lose the possibility of undefined #61225

Open
jcalz opened this issue Feb 19, 2025 · 0 comments Β· May be fixed by #61233
Open

Unions seem to sidestep --noUncheckedIndexedAccess and lose the possibility of undefined #61225

jcalz opened this issue Feb 19, 2025 · 0 comments Β· May be fixed by #61233

Comments

@jcalz
Copy link
Contributor

jcalz commented Feb 19, 2025

πŸ”Ž 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

πŸ’» 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. ( #47531 (comment) )

Ran into this when looking at a Stack Overflow question

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant