unistore icon indicating copy to clipboard operation
unistore copied to clipboard

Typings for function binding an action does not allow async

Open jschill opened this issue 3 years ago • 0 comments

When binding a function manually....

import createStore from 'unistore'

const store = createStore({ count: 0, stuff: [] })

const myAsyncAction = async (state, store) => {
  await somethingAsync()
  ....
};

...and do the actual binding

const myBoundAsyncAction = store.action(myAsyncAction)

It will result in.. Promise returned in function argument where a void return was expected.eslint[@typescript-eslint/no-misused-promises](https://typescript-eslint.io/rules/no-misused-promises) ...because the typings does not allow for an async function.

A suggested fix would be to type Action the same way as ActionFn but with the addition of Promise<void> (which should also be added to ActionFn i guess). Ie:

export type Action<K> = (state: K, ...args: any[]) => Promise<Partial<K>> | Partial<K> | Promise<void> | void;

jschill avatar Oct 07 '22 09:10 jschill