Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createSelector can't handle generic state #571

Open
Fiurgeist opened this issue Mar 30, 2022 · 6 comments
Open

createSelector can't handle generic state #571

Fiurgeist opened this issue Mar 30, 2022 · 6 comments

Comments

@Fiurgeist
Copy link

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

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

@markerikson
Copy link
Contributor

Noted, but unfortunately I'm not sure when I'm going to have time to look into this.

@chentsulin
Copy link
Contributor

chentsulin commented May 2, 2022

This case doesn't work either:

const createBazSelector = <S>(
  getFoo: Selector<S, number, never>
): Selector<S, string, never> => createSelector(getFoo, (_state) => 'bar', (_foo, _bar) => 'baz')

@mitoihs
Copy link

mitoihs commented Apr 6, 2023

Is it ever going to be fixed? It's not a big deal now to stick with 4.1.2 if there are no new major versions but sooner or later...

@markerikson
Copy link
Contributor

@mitoihs realistically, no.

The current TS types are needed to deal with a lot of other concerns, like making sure the argument types are inferred correctly. It would be nice if they handled this generics issue, but if it comes down to "have correct argument types" vs "handle generic state", the arguments are more important.

On top of that: I simply don't have time to look into this issue. Right now all my attention is focused on RTK 2.0. Now, as part of that I am starting to work on possible changes to the internals of Reselect for v5, but at the moment that does not include any planned work on the TS types. And, tbh, we have a very similar problem with createSlice. You can't do generic types for its payloads, because of TS limitations.

So, it's likely that this problem isn't even "fixable" given the current structure of the TS types.

If anyone else would like to try fixing this, please do so.

But I don't have time or priority to work on this, and my guess is that there isn't a "fix" without coming up with a completely different set of TS types.

@mitoihs
Copy link

mitoihs commented Apr 7, 2023

@markerikson thank you for an honest reply.

Could you elaborate a bit about we have a very similar problem with createSlice. You can't do generic types for its payloads, because of TS limitations.?
Because I think that maybe I've solved that issue in one of my projects. I have a createSlice wrapper that creates a slice with a generic state and a set of actions, and a user of that wrapper can add other actions and everything remains correctly typed. But I'm not sure if that is the case you called impossible.

@markerikson
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants