From c254a8abc73d7e2589acbffe432a33f985c1003d Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 9 Apr 2021 20:35:57 +0200 Subject: [PATCH] feat(types): fail on async patch --- src/types.ts | 7 +++++-- test-dts/store.test-d.ts | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/types.ts b/src/types.ts index bc81a256..a7a5a2a4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -76,9 +76,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 +}) -- 2.47.3