0

I just upgraded from typescript 4.1.2 to 4.3.2 using Rematch. These rematch packages are used:

  • "@rematch/core": "2.0.1"
  • "@rematch/select": "3.0.1"

and I faced a typescript error: Type never has no call signatures. I looked into the 3rd parties' code and found that RematchDispatch gives never type when I have a reducer and an effect in a particular model with the same name. For example:

export const myModel = createModel<RootModel>()({
  state: { ... },
  reducers: {
    myReducer(state, payload: string) { ... }
  },
  effects: dispatch => ({
    myReducer(payload: string, rootState) { ... }
  }
}

and RematchDispatch<RootModel>['myModel']['myReducer'] type will give 'never' type so for example I cannot call this via useDispatch in a react component because of the above-mentioned typescript error.

I also checked the documentation: https://rematchjs.org/docs/api-reference/models and found this

Effects functions that share a name with a reducer are called after their reducer counterpart.

This seems to be a valid case and it worked in my project until this typescript upgrade so this issue might be related to the Rematch and Typescript mix.

Could you please help me with this issue? What did I do in the wrong way?

Thanks in advance!

mway
  • 3
  • 1
  • 1
    share reprodcable exmaple – captain-yossarian from Ukraine Jul 05 '21 at 09:01
  • Here is codesandbox example: https://codesandbox.io/s/interesting-sky-wnt9p?file=/src/Count.tsx but in the browser, I could set Typescript 4.3.2, but if you export it into a zip file and run it locally you will see the error message: 'Type never has no call signatures'. With Typescript 4.1.2 there is no type check the increment can be called with anything. In the sample code you can see a string parameter instead of the 'number' and there is no warning from the compiler. I think calling never was allowed in Typescript 4.1.2 (like any). – mway Jul 05 '21 at 19:22

1 Answers1

0

I'm Sergio, Rematch Maintainer!

Looks you found an issue of our typing's system, actually TypeScript upgrade from 4.1 to 4.3 changed a bit how it handles several scenarios that made necessary an adjustment by Rematch team.

We're already working on this: https://github.com/rematch/rematch/issues/912, and also we already introduced a Github Action to tests our typings tests to Typescript@next version, in that way we can fix this issues before TypeScript releases his stable.

A possible workaround, is to name differently your effect and your reducer, like increment for the effect and INCREMENT in uppercase for the reducers.

Anyways, this should be fixed and we're already working on it :)

  • Hi Sergio, Thank you for the quick answer and the coming fix! What you described is OK, but in this case, we lose the essence of the effect meaning that it is immediately executed after calling the same-name reducer with the updated state. Until the fix, I managed to apply a workaround: I created my own dispatch type: export type AppDispatch = { [modelKey in keyof RootModel]: ExtractRematchDispatchersFromReducers } and used it. It had some consecuences such as no effect without reducer and some type castings. – mway Jul 06 '21 at 20:37
  • Yeah I understand the problem, we already merged a fix. I hope in more or less 1 week or two, release a new version. You can also patch the package types with a package like `patch-package` until its released. You can take a look how we fixed it here https://github.com/rematch/rematch/pull/913/files#diff-4c2458380e50c967c70d7afe819dc7412180f9f817043f27441de3498e4d0479 – Sergio Moreno Jul 07 '21 at 08:26
  • This is already fixed since https://github.com/rematch/rematch/releases/tag/%40rematch%2Fcore%402.1.0 – Sergio Moreno Sep 20 '21 at 22:07