Skip to content

createActions helper #224

@theKashey

Description

@theKashey

With actions being defined aside of createStore it might become a little tricky to type them.

In many cases one had to write something like

const actions = {
    resetTriggerPosition: (): Action<State> => () => ({ setState }) => {
            setState({  });
        },
}

The modern way to write the same with less TS involved can use satisties operator

const actions = {
    resetTriggerPosition: () => () => ({ setState }) => {
            setState({  });
        },
}  satisfies Record<
    string,
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (...args: any[]) => Action<State>
>;

Works, but is it making anything better?


what about creating a helper function createActions in the form of

const createActions =<TState extends object>(): <TActions extends Record<string, ActionThunk<TState, TActions>>(actions: TActions): TActions => actions;

That simple, the same interface as createStore accepts.

This fill change action definition to

```tsx
const actions = createActions<State>()({
    resetTriggerPosition: () => () => ({ setState }) => {
            setState({  });
        },
} )

Something like https://www.typescriptlang.org/play?#code/ATCWDsBcFMCcDMCGBjawDK1LsomAeAFRz2gD5gBvAKBBAApxoB3EmALmAAVFZJREAGyJtyASk4A3APagAJgG5awAL7VlkAJ4AHNAHEsokbhgUAvMHpjgZigCVoiOdPCDNx0mSUadaHNNhoAEFkfhcg7VAPUxsqZRAAZ0MTaE5MbBTo8iU6YABzZNJOAwzSLK944DlQBO08ZAALTiJgaAAPGHA5BOAQsPAsgBpgRHBNYdHNMjJ6SpAUfsIGgFdwAGtOQkrrW2AHSGXYcEJfIgqQFW8QLV1e0NAXfEriFMHngGEXXAg4LlhpbQ9CwyeRvXKEfaHcAANSEyzQwNkcmAAB9uP8ALY1aD4EFyMjKcyWSqISJpSABYL3cKRcpgujIL6IH6wP4AhKbT5QZlMVn-QHKHYUCFYKGwwTwq7AG5oPoPY4rdZPcGiekgQhylw9dqdbp7aCM2ByfAJSCwCB5YaahWrNZDYAa6ngBLTQmxegAOi9vDyHJGYyFd369smEzGFWojOdkGAyECpGtQKyrQ60C6PWkACMAFYGyAzCTAIiJlO6noOQ3G03m8CWoPypa2+2O-ou6b0Bbyv0truBzta7xR03S6DDixxxwwRP4ShtdjVi0qAv0GjXUeQdhWEC7FdJUowFSBveiFdzgCMw007AATIfqIeFEA

Wondering if with modern TS we can remove the first function call, it "bind" State variable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions