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
Inlining wrappers over createSelector causes selector args to be of any type #559
constselectComposedFlagInline=createSelector(createPreferenceSelector("featureA"),createPreferenceSelector("featureB"),(featureA,featureB)=>featureA&&featureB==="release"// ^^^^ ^^^^ Parameters implicitly has an 'any' type.);
Here createPreferenceSelector is our helper to build typed preference selectors. It's included into code sandbox example:
While it didn't look like a huge issue, it made me very curious about why inlining caused that issue. For me it looks like all three examples should work just fine.
After some testing it looks like this issue was introduced in 4.1.0.
Huh. I'll be honest, I have no idea what's going on here and am not really even sure how to start debugging this one :)
(This goes along with my Twitter comment the other day that "what we really need is a types-level debugger that would tell us how TS is analyzing and transforming types at each step in the process".)
Given that there seem to be workarounds and that I've got other stuff going on, I'm afraid this one is going to be relatively low priority, and I'm doubtful at first glance that I'd even be able to figure out what's happening or what a solution is.
I may toss it out on Twitter or something and see if anyone else has a clue what's going on here.
Yeah. If you look at the commit/issue history of this repo over the last few weeks, you can see that I (and a few others I've dragged in) have been iterating on the types repeatedly. Every time we fix one issue, something else pops up. I feel pretty good about the types in 4.1.5, tbh - they pass all the typetest scenarios we've got (and I've added a bunch of additional scenarios based on these issues), and based on the thought and design we've put into how they work, they seem to be doing what they should logically in combining parameters.
That process would have been much easier if I'd had better ways to inspect the intermediate steps of the types, though :)
Potentially the same issue also bites me on the other end — in the composed selector calculation, here's a small sample:
interfaceFoo<A>{foo: A}// some selectorfunctionfoos(s: RootState){return[]asFoo<string>[]}// extract loses type information when used with createSelectorfunctionextract<A>(items: Foo<A>[]){returnitems[0].foo}constfoosDerived=createSelector(foos,extract)// This type is unknown when it should be a FooconstshouldBeFooButIsUnknown=foosDerived(nullasany)
I believe we see this with both TS 4.4.2 and 4.5.2, hit it when upgrading from reselect 3.0.0 to 4.1.5
typescript$ tsc inference.ts --target es2017 --moduleResolution node
inference.ts:25:7 - error TS2322: Type 'unknown[]' is not assignable to type'Foo<unknown>[]'.
Property 'bar' is missing intype'{}' but required intype'Foo<unknown>'.
25 const thisShouldBeFoos: Foo[] = foosList(state)
~~~~~~~~~~~~~~~~
inference.ts:4:3
4 bar: A
~~~
'bar' is declared here.
Found 1 error.
Even though both calls are identical, there's just an extra function call in the working one. I see unknown[] as the type of thisShouldBeFoos in this example.
Think this is the result of using ReturnType<> directly on a function with generics (ReturnType is used in ExtractReturnType) which returns unknown. As far as I know this is a limitation on generics in typescript and the workarounds on this page are the best one can do at the moment.
4.1.8 - subtle change, the type is inferred to be unknown rather than any
4.1.0 - another change, type is inferred as SelectorResultArray<Selectors, Selectors>[0] but unclear what this collapses to (it still isn't inferred correctly).
I'd be thrilled if this was something that could be addressed, but given it is a 3+ year issue, I understand if it isn't prioritized. Thanks for the great library nonetheless!
EDIT
Ah, as a comment above mentioned this being a limitation for generics, that might be the problem.
Here is another playground that works, but I have to define the ReturnType explicitly:
changed the title [-]Inlining wrappers over createSelector causes selector args to be of any type[/-][+]Inlining wrappers over `createSelector` causes selector args to be of any type[/+]on Feb 26, 2025
changed the title [-]Inlining wrappers over `createSelector` causes selector args to be of any type[/-][+]Inlining wrappers over `createSelector` causes selector args to be of `any` type[/+]on Mar 18, 2026
Activity
markerikson commentedon Nov 30, 2021
Huh. I'll be honest, I have no idea what's going on here and am not really even sure how to start debugging this one :)
(This goes along with my Twitter comment the other day that "what we really need is a types-level debugger that would tell us how TS is analyzing and transforming types at each step in the process".)
Given that there seem to be workarounds and that I've got other stuff going on, I'm afraid this one is going to be relatively low priority, and I'm doubtful at first glance that I'd even be able to figure out what's happening or what a solution is.
I may toss it out on Twitter or something and see if anyone else has a clue what's going on here.
BlackFoks commentedon Nov 30, 2021
Thanks for the response! It's not urgent at all, the workarounds work fine for now.
It would be awesome to see that.
markerikson commentedon Nov 30, 2021
Yeah. If you look at the commit/issue history of this repo over the last few weeks, you can see that I (and a few others I've dragged in) have been iterating on the types repeatedly. Every time we fix one issue, something else pops up. I feel pretty good about the types in 4.1.5, tbh - they pass all the typetest scenarios we've got (and I've added a bunch of additional scenarios based on these issues), and based on the thought and design we've put into how they work, they seem to be doing what they should logically in combining parameters.
That process would have been much easier if I'd had better ways to inspect the intermediate steps of the types, though :)
garrettm commentedon Dec 1, 2021
Potentially the same issue also bites me on the other end — in the composed selector calculation, here's a small sample:
I believe we see this with both TS 4.4.2 and 4.5.2, hit it when upgrading from reselect 3.0.0 to 4.1.5
markerikson commentedon Dec 2, 2021
@garrettm what is the inferred type of
foosDerivedwhen you mouse over it, on both of the lines that it's used?Also, what happens if you call
foosDerived()with a meaningful type argument instead ofas any?garrettm commentedon Dec 2, 2021
@markerikson here's a more full repro:
in this example, I see this error:
Even though both calls are identical, there's just an extra function call in the working one. I see
unknown[]as the type ofthisShouldBeFoosin this example.markerikson commentedon Dec 2, 2021
@garrettm fwiw, while you're definitely seeing something, it looks to be a very different problem than what was originally described in this issue.
Pasting that example into the TS playground, the type of
foosListas it's being used is:So, somehow the final return type is the problem here, and I'm not sure where it's going wrong in the middle.
doarrison commentedon Dec 2, 2021
Think this is the result of using
ReturnType<>directly on a function with generics (ReturnTypeis used inExtractReturnType) which returns unknown. As far as I know this is a limitation on generics in typescript and the workarounds on this page are the best one can do at the moment.markerikson commentedon Dec 2, 2021
Huh. That... sounds very plausible, yeah.
aryaemami59 commentedon Mar 16, 2024
I don't think this is an issue anymore as the types were simplified and tested prior to v5.
romellem commentedon Feb 19, 2025
I just commented on this similar issue, but actually think this one describes what I am seeing with the current version 5.1.1 on TS 5.7.3
createSelectorwith inline function declarations passed as separate arguments #666 (comment)Here are TS Playground links for different versions of reselect reproducing the issue:
unknownrather thananySelectorResultArray<Selectors, Selectors>[0]but unclear what this collapses to (it still isn't inferred correctly).I'd be thrilled if this was something that could be addressed, but given it is a 3+ year issue, I understand if it isn't prioritized. Thanks for the great library nonetheless!
EDIT
Ah, as a comment above mentioned this being a limitation for generics, that might be the problem.
Here is another playground that works, but I have to define the ReturnType explicitly:
[-]Inlining wrappers over createSelector causes selector args to be of any type[/-][+]Inlining wrappers over `createSelector` causes selector args to be of any type[/+][-]Inlining wrappers over `createSelector` causes selector args to be of any type[/-][+]Inlining wrappers over `createSelector` causes selector args to be of `any` type[/+]aryaemami59 commentedon Mar 18, 2026
@romellem
In the TS Playground, you will get the intended behavior if you place the input selector in an array: