Skip to content

Inlining wrappers over createSelector causes selector args to be of any type #559

Description

@BlackFoks

When upgrading from 4.0.0 to 4.1.5, our selectors started to complain about any types when we inline our createSelector wrapper:

  • TS7006: Parameter 'featureA' implicitly has an 'any' type.
  • TS7006: Parameter 'featureB' implicitly has an 'any' type.

TypeScript version is 4.4.2.

CodeSandbox example

Here's a code sandbox example of the issue (line 66):
https://codesandbox.io/s/reselect-typescript-issues-xsgeq?file=/src/App.tsx

Description

Code example:

const selectComposedFlagInline = 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:

function createPreferenceSelector<P extends PrefName>(prefName: P) {
  return (state: AppState) => selectPreferenceValue(state, prefName);
}

Extracting preference selector into its own variable solves the issue:

const selectFeatureA = createPreferenceSelector("featureA");
const selectFeatureB = createPreferenceSelector("featureB");

const selectComposedFlag = createSelector(
  selectFeatureA,
  selectFeatureB,
  (featureA, featureB) => featureA && featureB === "release"
);

Removing the helper entirely also solves the issue:

const selectComposedFlagNoHelper = createSelector(
  (state: AppState) => selectPreferenceValue(state, "featureA"),
  (state: AppState) => selectPreferenceValue(state, "featureB"),
  (featureA, featureB) => featureA && featureB === "release"
);

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.

Activity

  1. markerikson commented on Nov 30, 2021

    @markerikson
    Contributor

    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.

  2. BlackFoks commented on Nov 30, 2021

    @BlackFoks
    Author

    Thanks for the response! It's not urgent at all, the workarounds work fine for now.

    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

    It would be awesome to see that.

  3. markerikson commented on Nov 30, 2021

    @markerikson
    Contributor

    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 :)

  4. garrettm commented on Dec 1, 2021

    @garrettm

    Potentially the same issue also bites me on the other end — in the composed selector calculation, here's a small sample:

    interface Foo<A> {
      foo: A
    }
    
    // some selector
    function foos(s: RootState) {
      return [] as Foo<string>[]
    }
    
    // extract loses type information when used with createSelector
    function extract<A>(items: Foo<A>[]) {
      return items[0].foo
    }
    
    const foosDerived = createSelector(foos, extract)
    
    // This type is unknown when it should be a Foo
    const shouldBeFooButIsUnknown = foosDerived(null as any)

    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

  5. markerikson commented on Dec 2, 2021

    @markerikson
    Contributor

    @garrettm what is the inferred type of foosDerived when 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 of as any?

  6. garrettm commented on Dec 2, 2021

    @garrettm

    @markerikson here's a more full repro:

    import {createSelector} from 'reselect'
    
    interface Foo<A = unknown> {
      bar: A
    }
    
    function values<A>(_: {[k: string]: A}): A[] {
      const result: A[] = []
      const ks = Object.keys(_)
      for (const key of ks) {
        result.push(_[key])
      }
      return result
    }
    
    interface State {
      foos: {[k: string]: Foo<string>}
    }
    
    const state: State = {foos: {}}
    const foos = (s: State) => s.foos
    
    const foosList = createSelector(foos, values)
    const thisShouldBeFoos: Foo[] = foosList(state)
    
    const foosListWorking = createSelector(foos, f => values(f))
    const thisIsFoos: Foo[] = foosListWorking(state)

    in this example, I see this error:

    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 in type '{}' but required in type '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.

  7. markerikson commented on Dec 2, 2021

    @markerikson
    Contributor

    @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 foosList as it's being used is:

    const foosList: (state: {
        foos: {
            [k: string]: Foo<string>;
        };
    }) => unknown[]

    So, somehow the final return type is the problem here, and I'm not sure where it's going wrong in the middle.

  8. doarrison commented on Dec 2, 2021

    @doarrison

    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.

  9. markerikson commented on Dec 2, 2021

    @markerikson
    Contributor

    Huh. That... sounds very plausible, yeah.

  10. aryaemami59 commented on Mar 16, 2024

    @aryaemami59
    Member

    I don't think this is an issue anymore as the types were simplified and tested prior to v5.

  11. added
    TypeScript
    on Jan 3, 2025
  12. romellem commented on Feb 19, 2025

    @romellem

    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

    Here are TS Playground links for different versions of reselect reproducing the issue:

    • 5.1.1
    • 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).
    • 4.0.0 - it works here!

    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:

  13. 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
  14. 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
  15. aryaemami59 commented on Mar 18, 2026

    @aryaemami59
    Member

    @romellem

    In the TS Playground, you will get the intended behavior if you place the input selector in an array:

    createSelector(
    -	createSelectorBoundToName(getAgeForName, 'alice'),
    +	[createSelectorBoundToName(getAgeForName, 'alice')],
    	(age) => {
    		// Incorrectly inferred to be `any`
    		const checkType = age;
            //    ^?
    	}
    );
  16. added theissue type on Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    TypeScript

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions