-
-
Notifications
You must be signed in to change notification settings - Fork 419
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
fix: reducers and effects with same name are correctly typed 4.3.X #913
Conversation
size-limit report 📦
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice utility type. Good job mate. Do we need to add a notice about this case: if the same name used in both reducer and effects, the reducer will be executed first, and then the effects:
function createEffectsMiddleware<
TModels extends Models<TModels>,
TExtraModels extends Models<TModels>
>(bag: RematchBag<TModels, TExtraModels>): Middleware {
return (store) => (next) => (action: Action): any => {
if (action.type in bag.effects) {
// first run reducer action if exists
next(action)
// then run the effect and return its result
return (bag.effects as any)[action.type](
action.payload,
store.getState(),
action.meta
)
}
return next(action)
}
}
I can't figure out this executing order until I read the above source code.
Or we should remind our users not to use this way which I think is confusing. In my opinion, we can use a more semantic name on effects if it conflicts with the reducer. |
Technically, our recommendation is to use Uppercase for reducers and effects with camelcase in that way you can differentiate easily on Redux Devtools. But... as we accept reducers and effects with the same name, we have to accept that on typings too |
oh, i thought it was the inverse... let me check |
@tianzhich Just introduced a new test to check the order of execution and don't introduce any regression there, also i replaced the order of the merge exclusive to give priority to reducers type. Where the scenario of: state: {
count: 0,
},
reducers: {
add(state, payload: string) {
return state
},
},
effects: () => ({
add(payload: number) {
},
}), store.dispatch.add() will type the payload as string |
well, now it's no problem for me. |
&
Resolves #912
Before 4.3 TypeScript accepted "duplicated" types but now is more stricter, so we just merge them giving priority always to reducers because is the entrypoint (will be the first executed if the same is duplicated)