You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Empty array in union with tuple doesn't infer undefined on indexed access #47531
Playground
Note that noUncheckedIndexedAccess is enabled - but doesn't make any difference in this case.
The reason it is a problem is that [] is inferred as never[] in many scenarios, thus allowing type-unsafe code. Example:
const data = ['A'] as const;
// inferred as `never[] | ['A']
const a = Math.random() > 0.5 ? [] : data;
a[0].length; // can be undefined, but no compiler error
changed the title [-]Empty tuple in union doesn't infer undefined on indexed access[/-][+]Empty array in union doesn't infer undefined on indexed access[/+]on Jan 20, 2022
changed the title [-]Empty array in union doesn't infer undefined on indexed access[/-][+]Empty array in union with tuple doesn't infer undefined on indexed access[/+]on Jan 20, 2022
From the practical standpoint, if I know I need this workaround here and now, it means I anticipate possible type unsafety and have double checked my code already. However, if I'm unaware I'm doing something wrong, it would also not occur to me to do as const.
So my point is that this should be as automated (inferred) as possible.
#50474 was marked as a duplicate of this, but if so, then the problem doesn't seem to have much to do with never[], and has more to do with propagation of the undefined (when --noUncheckedIndexedAccess is enabled) when reading properties from unions where some union members have index signatures and some do not. Like, what do we make of this behavior:
Activity
[-]Empty tuple in union doesn't infer undefined on indexed access[/-][+]Empty array in union doesn't infer undefined on indexed access[/+]amakhrov commentedon Jan 20, 2022
This would not be a problem if an empty array was inferred as
[](empty tuple) instead ofnever[][-]Empty array in union doesn't infer undefined on indexed access[/-][+]Empty array in union with tuple doesn't infer undefined on indexed access[/+]fatcerberus commentedon Jan 20, 2022
Workaround:
amakhrov commentedon Jan 20, 2022
Bruce Pascoe (@fatcerberus) yup.
From the practical standpoint, if I know I need this workaround here and now, it means I anticipate possible type unsafety and have double checked my code already. However, if I'm unaware I'm doing something wrong, it would also not occur to me to do
as const.So my point is that this should be as automated (inferred) as possible.
RyanCavanaugh commentedon Jan 20, 2022
This is pretty messed up; we shouldn't be presenting a
never[]under normal circumstances.fatcerberus commentedon Jan 22, 2022
Ryan Cavanaugh (@RyanCavanaugh) A surefire way to get a
never[]is to simply return[]from a function:jcalz commentedon Aug 28, 2022
#50474 was marked as a duplicate of this, but if so, then the problem doesn't seem to have much to do with
never[], and has more to do with propagation of theundefined(when--noUncheckedIndexedAccessis enabled) when reading properties from unions where some union members have index signatures and some do not. Like, what do we make of this behavior:Playground link
Is this a bug? If so, does it belong here in #47531 or somewhere else?