#571·reselect

`createSelector` can't handle generic state

Author: FiurgeistCreated Mar 30, 2022Updated Mar 18, 2026
LabelsTypeScript

I need a generic here, since I have shared functions that are used in several Apps with different RootStates. I boiled it down to:

typescript
import { createSelector, Selector } from "reselect";

export const createBarSelector = <S>(
  getFoo: Selector<S, number, never>
): Selector<S, string, never> => createSelector(getFoo, (_foo) => "bar");

This works up until version 4.1.2. From 4.1.3 onwards the following TypeScript error happens:

(alias) createSelector<[(state: S) => string], string>(items_0: (state: S) => string, items_1: (args_0: string) => string): ((state: IgnoreInvalidIntersections<IfJustNullish<S, unknown, S>> extends IgnoreInvalidIntersections<...> ? Expand<...> : never) => string) & OutputSelectorFields<...> & { ...; } (+2 overloads) import createSelector

Input selectors as separate inline arguments

Type '((state: IgnoreInvalidIntersections<IfJustNullish<S, unknown, S>> extends IgnoreInvalidIntersections<IfJustNullish<S, unknown, S>> ? Expand<...> : never) => string) & OutputSelectorFields<...> & { ...; }' is not assignable to type '(state: S) => string'. Types of parameters 'state' and 'state' are incompatible. Type 'S' is not assignable to type 'IgnoreInvalidIntersections<IfJustNullish<S, unknown, S>> extends IgnoreInvalidIntersections<IfJustNullish<S, unknown, S>> ? Expand<...> : never'.ts(2322)

Versions

TS: 4.6.2 Reselect: 4.1.5