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

Throw error when custom action creator does not have type value #3570

Closed
chawes13 opened this issue Jun 29, 2023 · 1 comment · Fixed by #3572
Closed

Throw error when custom action creator does not have type value #3570

chawes13 opened this issue Jun 29, 2023 · 1 comment · Fixed by #3572
Milestone

Comments

@chawes13
Copy link
Contributor

It'd be great to have a guard for passing in an action creator that doesn't have a truthy type string value.

I encountered this issue when migrating over to RTK. We built some custom action creators that we use in combination with a custom api middleware (we're not ready to switch to RTQ just yet - baby steps 😅!). While these have the toString() override, the builder addCase method is not using that and it seems like this override is getting deprecated in v2.

The following led to a fairly subtle bug, since it fails silently.

extraReducers: (builder) => {
  builder.addCase(apiActions.fetchTodo, todoHandler) // fetchTodo.type === undefined
}

Since I'm working in JS, this issue is easier to run into. Although, I believe a guard may still be helpful to prevent accidental assignments of an empty string to the type attribute in the implementation of a custom action creator in TS.

const type =
typeof typeOrActionCreator === 'string'
? typeOrActionCreator
: typeOrActionCreator.type
if (type in actionsMap) {
throw new Error(
'addCase cannot be called with two reducers for the same action type'
)
}
actionsMap[type] = reducer
return builder

@markerikson markerikson added this to the 1.9.x milestone Jun 29, 2023
@EskiMojo14
Copy link
Collaborator

I'm sure we'd be open to a PR that throws an error if the type variable is undefined :)

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

Successfully merging a pull request may close this issue.

3 participants