-
Notifications
You must be signed in to change notification settings - Fork 60
Open
Labels
enhancementNew feature or requestNew feature or request
Description
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({ });
},
} )
Wondering if with modern TS we can remove the first function call, it "bind" State variable.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request