From b2c02f083055454eb7d3f02d2dc588a9c52f28a9 Mon Sep 17 00:00:00 2001 From: Edoardo Ranghieri Date: Wed, 14 Aug 2024 19:06:55 +0200 Subject: [PATCH] fix(types): allow omitting input in `executeOnMount` when it's undefined --- packages/next-safe-action/src/hooks-utils.ts | 2 +- packages/next-safe-action/src/hooks.types.ts | 9 +++++---- website/docs/types.md | 9 +++++---- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/next-safe-action/src/hooks-utils.ts b/packages/next-safe-action/src/hooks-utils.ts index b770af8e..7021d48f 100644 --- a/packages/next-safe-action/src/hooks-utils.ts +++ b/packages/next-safe-action/src/hooks-utils.ts @@ -63,7 +63,7 @@ export const useExecuteOnMount = ( React.useEffect(() => { const t = setTimeout(() => { if (args.executeOnMount && !mounted.current) { - args.executeFn(args.executeOnMount.input); + args.executeFn(args.executeOnMount.input as S extends Schema ? InferIn : void); mounted.current = true; } }, args.executeOnMount?.delayMs ?? 0); diff --git a/packages/next-safe-action/src/hooks.types.ts b/packages/next-safe-action/src/hooks.types.ts index 486408db..f2b47d8c 100644 --- a/packages/next-safe-action/src/hooks.types.ts +++ b/packages/next-safe-action/src/hooks.types.ts @@ -6,10 +6,11 @@ import type { MaybePromise, Prettify } from "./utils.types"; * Type of base utils object passed to `useAction`, `useOptimisticAction` and `useStateAction` hooks. */ export type HookBaseUtils = { - executeOnMount?: { - input: S extends Schema ? InferIn : undefined; - delayMs?: number; - }; + executeOnMount?: (undefined extends S + ? { input?: undefined } + : { + input: S extends Schema ? InferIn : undefined; + }) & { delayMs?: number }; }; /** diff --git a/website/docs/types.md b/website/docs/types.md index cb7f3029..88862f2e 100644 --- a/website/docs/types.md +++ b/website/docs/types.md @@ -316,10 +316,11 @@ Type of base utils object passed to `useAction`, `useOptimisticAction` and `useS ```typescript export type HookBaseUtils = { - executeOnMount?: { - input: S extends Schema ? InferIn : undefined; - delayMs?: number; - }; + executeOnMount?: (undefined extends S + ? { input?: undefined } + : { + input: S extends Schema ? InferIn : undefined; + }) & { delayMs?: number }; }; ```