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
Incorrect behavior of ArrayLike<infer T> | Iterable<infer T>? #49764
I was digging into the behavior of the Array-related types when I was making up a good definition of Array.isArray for, again, uhyo/better-typescript-lib.
I've edited the comment. Hopefully it's clearer now.
For the first 3 types, if Foo0 holds, I suspect that Bar0 and Baz0 also hold.
For the last type, Baz1, I can't understand why it consists of an unknown[].
I see. I believe this misunderstanding is what infer does when the same type variable appears in multiple positions.
What you're expecting is that these utterances act as separate paths that the checker can take, but that's not how it works. When you have the same infer T, TS is trying to find a type that fits both instances at once. It's effectively the same as if you had written
which fails because there are three candidates collected for T - string, string, and number.
Baz1 includes an unknown because when zero candidates get collected for T or U respectively, the resulting inference is unknown. An equivalent example would be
Activity
RyanCavanaugh commentedon Jul 5, 2022
This all looks correct to me. Can you make a more explicit case for why you consider this wrong?
Aside: it's very confusing to have same-named variables in the same small repro
graphemecluster commentedon Jul 6, 2022
I was digging into the behavior of the
Array-related types when I was making up a good definition ofArray.isArrayfor, again, uhyo/better-typescript-lib.I've edited the comment. Hopefully it's clearer now.
For the first 3 types, if
Foo0holds, I suspect thatBar0andBaz0also hold.For the last type,
Baz1, I can't understand why it consists of anunknown[].RyanCavanaugh commentedon Jul 7, 2022
I see. I believe this misunderstanding is what
inferdoes when the same type variable appears in multiple positions.What you're expecting is that these utterances act as separate paths that the checker can take, but that's not how it works. When you have the same
infer T, TS is trying to find a type that fits both instances at once. It's effectively the same as if you had writtenwhich fails because there are three candidates collected for
T-string,string, andnumber.Baz1includes an unknown because when zero candidates get collected forTorUrespectively, the resulting inference isunknown. An equivalent example would beThere's nothing to occur here which would exclude the
{ bb: B }part of the union.typescript-bot commentedon Jul 10, 2022
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.