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
Destructuring array result of function with generics call sometimes lead to type loss #32003
Search Terms: destructuring result function generics type loss any
Problem is only with snippet 2, when I get the third parameter with destructuring, the first one became any instead of the generic type. The problem doesn't show if the generic is explicitly provided. Snippets 1, 3 and 4 works as expected.
Related Issues:
Some issues are covering topics that seems related, but I couldn't find one with my specific issue, sorry if there's already a duplicate for this one!
This one is a bit strange. In examples 1 and 2, the call to foo() is contextually typed by the implied types of the array destrucuring patterns ([any, any] and [any, any, any] respectively). Since foo is generic and we have a contextual type, we perform generic return type inference (#16072). In example 2 where the arities of the tuple types match we then infer any for T, and since there is nothing else to make inferences from, that's what we end up with. In the other examples, we make no inferences for T at all and therefore end up with the default {}.
Ideally we shouldn't make any inferences from the implied types of the destructuring patterns, but in other scenarios we do want a contextual type of a tuple type to indicate that array literals should be treated as tuples. So we have conflicting goals and there isn't an obvious simple fix. Add to that the anti-pattern of having a type parameter that isn't used in anywhere in the parameter list (it's basically just a disguised type assertion) and I think we'll call this a design limitation.
Activity
ahejlsberg commentedon Jun 21, 2019
This one is a bit strange. In examples 1 and 2, the call to
foo()is contextually typed by the implied types of the array destrucuring patterns ([any, any]and[any, any, any]respectively). Sincefoois generic and we have a contextual type, we perform generic return type inference (#16072). In example 2 where the arities of the tuple types match we then inferanyforT, and since there is nothing else to make inferences from, that's what we end up with. In the other examples, we make no inferences forTat all and therefore end up with the default{}.Ideally we shouldn't make
anyinferences from the implied types of the destructuring patterns, but in other scenarios we do want a contextual type of a tuple type to indicate that array literals should be treated as tuples. So we have conflicting goals and there isn't an obvious simple fix. Add to that the anti-pattern of having a type parameter that isn't used in anywhere in the parameter list (it's basically just a disguised type assertion) and I think we'll call this a design limitation.miloszpp commentedon Aug 22, 2019
Just wanted to point out that this issue impacts typing hooks in React.
The type of
pillis inferred toanywhile it should beundefinedaccording to the type definition:danieloprado commentedon Sep 10, 2019
A little workaround:
TLadd commentedon Feb 6, 2020
Ran into this as well:
So in my case the generic does appear in the arguments but it is an optional parameter.
dis properly typed asSizeswhilebis of typeany.Using overrides is another solution that fixed this for me:
luokuning commentedon Mar 25, 2020
Looks like TypeScript v3.8 have fixed that issue?
mrblackus commentedon Jun 24, 2020
In can confirm that issue is fixed in both TS 3.8 and 3.9. Thanks!