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

@rematch/select typing problem with createSelector() function #966

Open
klvnptr opened this issue Feb 13, 2022 · 0 comments
Open

@rematch/select typing problem with createSelector() function #966

klvnptr opened this issue Feb 13, 2022 · 0 comments

Comments

@klvnptr
Copy link

klvnptr commented Feb 13, 2022

Describe the bug

A selector function within the model's "selectors" key should return a type similar to this

Selector<RootState, number>

(Let's assume the selector extracts a Product array from the RootState)
We have this RootModel and RootState

export interface RootModel extends Models<RootModel> {
  app: typeof app;
}
export type RootState = RematchRootState<RootModel>;

We have this model:

export const app = createModel<RootModel>()({
  name: "app",
  state: initialState,
  selectors: (slice, createSelector, hasProps) => ({
    productsOk(): Selector<RootState, number> {
      return slice((state) => state.menu.products.length);
    },
    products: (): Selector<RootState, number> => {
      const cs = createSelector(
        (state: RootState): MyAppState => state.app,
        (as) => as.menu.products.length
      );
      return cs;
    },
  }),
  reducers: { ... },
});

The problem is with the variable "cs", since it's type is, which can be clearly seen in VSCode by hovering over the mouse. It is inferred by TS using the createSelector function:

const cs: ((state: never) => number) & OutputSelectorFields<(args_0: MyAppState) => number & {
    clearCache: () => void;
}> & {
    clearCache: () => void;
}

Since it's state parameter's type is never, it is never gonna be compatible with Selector<RootState, number>.
We get the following TS error:

Type '((state: never) => number) & OutputSelectorFields<(args_0: MyAppState) => number & { clearCache: () => void; }> & { clearCache: () => void; }' is not assignable to type '(state: RootState) => number'.
  Types of parameters 'state' and 'state' are incompatible.
    Type 'RootState' is not assignable to type 'never'.ts(2322)

Looks like the problem is with defaulting TExtraModels to Record<string, never>. createSelector function infers never type for state:

export type RematchRootState<
	TModels extends Models<TModels>,
	TExtraModels extends Models<TModels> = Record<string, never>
> = ExtractRematchStateFromModels<TModels, TExtraModels>

I'm using the latest 4.5.x TypeScript.
Interestingly productsOk selector has no problem with typing.
Also if I define the RootStatePure like this:

export type RootStatePure = {
  [modelKey in keyof RootModel]: RootModel[modelKey]["state"];
};

Then the following works and TS has no typing problem:

export const app = createModel<RootModel>()({
  name: "app",
  state: initialState,
  selectors: (slice, createSelector, hasProps) => ({
    products: (): Selector<RootState, number> => {
      const cs = createSelector(
        (state: RootStatePure): MyAppState => state.app,
        (as) => as.menu.products.length
      );
      return cs;
    },
  }),
  reducers: { ... },
});
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

1 participant