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
Type narrowing not working for unions of tuples with object literals #31613
changed the title [-]Type narrowing for unions of tuple elements with object literals[/-][+]Type narrowing not working for unions of tuples with object literals[/+]on May 28, 2019
I would expect the normalised variable to have a type of [ FieldArray, FieldSet ] due to the type checking, although it's showing in my editor as Record<string, any>[].
Probably same as #12849
I've narrowed the issue down a little bit, as to where it breaks down: playground link
type DataFetchingResponse2 = [number, 'loaded'] | [null, 'error' | 'loading'];
function test2(): DataFetchingResponse2 {
return [null, 'error'];
}
function testTypeNarrowing() {
const res2 = test2();
// This works fine and correctly narrows res2[0] down to number
if (res2[1] == 'loaded') {
const data2 = res2[0];
}
// This does not, but casts it as a number|null
const status = res2[1]
if (status == 'loaded') {
const data2 = res2[0];
}
// This also does not, as it thinks dataFromDestructuring is number|null inside the type narrowing
const [dataFromDestructuring, statusFromDestructuring] = res2
if (statusFromDestructuring == 'loaded') {
console.log(dataFromDestructuring)
}
}
So narrowing directly on the tuple type works fine, but when destructuring or assigning to an intermediate variable, it does not narrow correctly.
Gustav Wengel (@GeeWee): I think that those are effectively two separate bugs, as the issue describes type narrowing without destructuring on tuples with primitive (working) and non-primitive (not working) values.
It probably could be done but would require us to either (1) re-think handling of unions of object types or (2) how to relate entity-named discriminants that share a prefix.
3 years old, still not fixed. Is this issue dead? It was first scheduled for 3.7 and then repeatedly rescheduled until it ended in the backlog. Fixing this would help in several use cases and greatly improve expressivity. I have several parts in my code base where I need to fall back to very generic types that are not helpful at all what could be a union of tuples.
Reacted by Ryan Cavanaugh, Trevin Miller, David, Marko Gartnar and Maksim Sinik
This is a duplicate of #18758. Both reports request that checking a nested discriminant narrow the containing union; tuple positions versus named outer properties do not change that underlying control-flow narrowing limitation.
Activity
[-]Type narrowing for unions of tuple elements with object literals[/-][+]Type narrowing not working for unions of tuples with object literals[/+]midzdotdev commentedon Aug 1, 2019
Hi guys, not sure if this helps but here's another example which I believe to be the same issue:
I would expect the normalised variable to have a type of
[ FieldArray, FieldSet ]due to the type checking, although it's showing in my editor asRecord<string, any>[].I found a workaround by doing this:
squidfunk commentedon Aug 9, 2019
Any update? Fixing this issue would make tuples much more powerful :-)
GeeWee commentedon Sep 3, 2019
Probably same as #12849
I've narrowed the issue down a little bit, as to where it breaks down:
playground link
So narrowing directly on the tuple type works fine, but when destructuring or assigning to an intermediate variable, it does not narrow correctly.
squidfunk commentedon Sep 3, 2019
Gustav Wengel (@GeeWee): I think that those are effectively two separate bugs, as the issue describes type narrowing without destructuring on tuples with primitive (working) and non-primitive (not working) values.
GeeWee commentedon Sep 3, 2019
I think you're probably right actually.
DanielRosenwasser commentedon Sep 3, 2019
Looks like this is probably a bug in
isMatchingReference.Kingwl commentedon Sep 13, 2019
I'd like to try this if no one started work
sandersn commentedon Sep 13, 2019
Sounds good, I wasn't planning to start on it until after the beta starts.
4 remaining items
sandersn commentedon Jan 10, 2020
This is actually not specific to tuples. Control-flow narrowing doesn't work with any nested properties:
It probably could be done but would require us to either (1) re-think handling of unions of object types or (2) how to relate entity-named discriminants that share a prefix.
Remove unnecessary invariant in favor of explicit cast
jtbandes commentedon Jun 21, 2022
Possibly related: #48378
squidfunk commentedon Jun 22, 2022
3 years old, still not fixed. Is this issue dead? It was first scheduled for 3.7 and then repeatedly rescheduled until it ended in the backlog. Fixing this would help in several use cases and greatly improve expressivity. I have several parts in my code base where I need to fall back to very generic types that are not helpful at all what could be a union of tuples.
rauschma commentedon May 3, 2025
The following doesn’t work either:
Useful when using tuples to help with overloading:
RyanCavanaugh commentedon Sep 2, 2026
This is a duplicate of #18758. Both reports request that checking a nested discriminant narrow the containing union; tuple positions versus named outer properties do not change that underlying control-flow narrowing limitation.