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
I've come across a case where the ordering of generic function's parameters changes how it infers the return type.
🔎 Search Terms
function generic type inference parameter order
🕗 Version & Regression Information
This is the behavior in every version I tried except for 3.3.3 where instead of unknown[], {}[] is inferred (which is even more wrong so at least we have some progress 😄).
inline and piped2 are inferred as number[] (like inline2 and piped).
You can see that alternateCollect just reverses the ordering of the parameters of the curried collect function. Ideally the ordering of the function parameters wouldn't affect the type inference. I would expect TypeScript to figure out the more specific type either way.
Also, the fact that this function is curried and that no matter which parameter ordering I chose one of the inferred return types (depending on the calling style) is wrong makes this an unfortunate situation.
I've also tried sprinkling NoInfer around to get Value inferred to a narrower type before Collection, but I couldn't get anything to work.
The text was updated successfully, but these errors were encountered:
inline is still inferred as number[] (instead of Set<number>) because it's always passing along whatever iterable is.
I wasn't super clear about the intended behavior so no worries though!
Also, now that I'm looking at this problem again I wonder if another way to solve this would involve higher kinded types (#1213). Sketch of the idea using made up syntax:
Bug Report
I've come across a case where the ordering of generic function's parameters changes how it infers the return type.
🔎 Search Terms
function generic type inference parameter order
🕗 Version & Regression Information
This is the behavior in every version I tried except for 3.3.3 where instead of
unknown[]
,{}[]
is inferred (which is even more wrong so at least we have some progress 😄).⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
inline
andpiped2
are inferred asunknown[]
.🙂 Expected behavior
inline
andpiped2
are inferred asnumber[]
(likeinline2
andpiped
).You can see that
alternateCollect
just reverses the ordering of the parameters of the curriedcollect
function. Ideally the ordering of the function parameters wouldn't affect the type inference. I would expect TypeScript to figure out the more specific type either way.Also, the fact that this function is curried and that no matter which parameter ordering I chose one of the inferred return types (depending on the calling style) is wrong makes this an unfortunate situation.
I've also tried sprinkling
NoInfer
around to getValue
inferred to a narrower type beforeCollection
, but I couldn't get anything to work.The text was updated successfully, but these errors were encountered: