question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to create request action generator?

See original GitHub issue

Hello, 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:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
marceleq27commented, Aug 23, 2021

I figured it out and thats work 😃

type Types = "Success" | "Error" | "Done"

type SnakeToCamelCase<S extends string> = S extends `${infer T}_${infer U}` ? `${Lowercase<T>}${Capitalize<Lowercase<SnakeToCamelCase<U>>>}` : S

type Output<T extends string> = {
    [key in `${SnakeToCamelCase<T>}${Types}`]: AnyAction
}

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

0reactions
the-dr-lazycommented, Aug 23, 2021
type Types = "Success" | "Error" | "Done"

type Output<a> = {
    [key in `${SnakeCaseToCamelCase<a>}${Types}`]: AnyAction
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found