Skip to content

How to Pass Generics to createSelector With Arguments? #742

Open

Description

@elsonel
export const selectFormValues = createSelector<
  [
    (state: RootState) => RootState['sliceValues'],
    (state: RootState, type: T) => T
  ],
  TypeToFormMap[T]
>(
  [
    (state) => state.sliceValues,
    (state, type) => type,
  ],
  (sliceValues, type) => sliceValues.formValuesMap[type]
);

Where could I define the generic T for something like this?

// Usage:

// TypeToFormMap['goal-form']
const values = useSelector((state: RootState) => selectFormValues(state, 'goal-form')

Activity

  1. EskiMojo14 commented on Mar 10, 2025

    @EskiMojo14
    Contributor

    due to typescript limitations, reselect selectors cannot be generic (and you shouldn't be passing type parameters to them)

    with that said, your example doesn't even need reselect:

    export const selectFormValues = <T extends keyof TypeToFormMap>(state: RootState, type: T) => state.sliceValues.formValuesMap[type]

    There's nothing being derived, so there's no memoisation needed

  2. changed the title [-]How to Pass Generics to createSelector With Arguments?[/-] [+]How to Pass Generics to `createSelector` With Arguments?[/+] 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

    TypeScriptIssues related to TypeScript.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions