Is there a way to create request action generator?
See original GitHub issueHello, I want to generate a few side actions and I wrote a generator function but TS still complaining 😃 Here is a small example:
const createRequestAction = (name: string) => {
const actionName = name
.split("_")
.map((item, i) => {
if (i === 0) {
return item.toLowerCase()
}
return item.charAt(0).toUpperCase() + item.slice(1).toLowerCase()
})
.join("")
return {
[`${actionName}Success`]: createActionCreator(`${name}_SUCCESS`),
[`${actionName}Error`]: createActionCreator(`${name}_ERROR`),
[`${actionName}Done`]: createActionCreator(`${name}_DONE`),
}
}
For example: createRequestAction("SET_AS_FAVOURITE") generate object with { setAsFavouriteSuccess: ActionType, setAsFavouriteError: ActionType, setAsFavouriteDone: ActionType }
and I’m exporting actions like this:
const setAsFavourite = createActionCreator("SET_AS_FAVOURITE", (resolve) => (id: string, isFavourite: boolean) => resolve({ id, isFavourite }))
const exampleActions = {
setAsFavourite,
...createRequestAction("SET_AS_FAVOURITE"),
}
export default exampleActions
And now in reducer, TS doesn’t recognize names like actions.setAsFavouriteSuccess, is there a way to type this function correctly?
Thanks 😃
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Free Website Call To Action Generator
Copy generators are the perfect free tools to help creatives spark inspiration on the never-ending quest to create the perfect call to action...
Read more >Create an action input generator
On the Recommended Next Best Action form, click Create Action Input Generator. · To create a new subflow, click Create New Input Generator...
Read more >createAction
The createAction helper combines these two declarations into one. It takes an action type and returns an action creator for that type.
Read more >Generators for reducer and action in Redux
Similarly, in the case of reducers, we can write a generic function createReducer, which will create generic reducer with common states (REQUEST ......
Read more >Redux Saga — How to make real good things with ...
Sagas using yield keyword and it's ability to halt execution within a function. So, by writing generator you just write steps necessary to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I figured it out and thats work 😃
I’ve attached code, maybe somebody also need it. Thank you very much again for help 😃