From: Eduardo San Martin Morote Date: Fri, 9 Apr 2021 18:35:57 +0000 (+0200) Subject: feat(types): fail on async patch X-Git-Tag: v0.4.0~20 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fe58fab1bdf95a4924672459aec88b1f4f87d0ba;p=thirdparty%2Fvuejs%2Fpinia.git feat(types): fail on async patch --- diff --git a/src/types.ts b/src/types.ts index fbb84982..8ed83209 100644 --- a/src/types.ts +++ b/src/types.ts @@ -61,9 +61,12 @@ export interface StoreWithState { * Sets or arrays and applying an object patch isn't practical, e.g. appending * to an array. * - * @param stateMutator - function that mutates `state` + * @param stateMutator - function that mutates `state`, cannot be async */ - $patch(stateMutator: (state: S) => void): void + $patch void>( + // this prevents the user from using `async` which isn't allowed + stateMutator: ReturnType extends Promise ? never : F + ): void /** * Resets the store to its initial state by building a new state object. diff --git a/test-dts/store.test-d.ts b/test-dts/store.test-d.ts index 8105e2f7..5350092b 100644 --- a/test-dts/store.test-d.ts +++ b/test-dts/store.test-d.ts @@ -21,3 +21,11 @@ store.nonExistant // @ts-expect-error store.nonExistant.stuff + +// @ts-expect-error cannot return a value +store.$patch(async () => {}) +store.$patch(() => {}) +store.$patch(() => { + // return earlier + return +})