Skip to content

Commit

Permalink
refactor(types): add infer types
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Aug 18, 2024
1 parent 5d4fd52 commit 08831e2
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/hooks.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { SafeActionFn } from "next-safe-action";
import type { Infer, Schema } from "next-safe-action/adapters/types";
import type {
HookBaseUtils,
Expand Down Expand Up @@ -55,9 +56,36 @@ export type UseHookFormOptimisticActionHookReturn<
Data,
State,
FormContext = any,
> = {
> = Omit<UseHookFormActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data, FormContext>, "action"> & {
action: UseOptimisticActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data, State>;
form: UseFormReturn<S extends Schema ? Infer<S> : any, FormContext>;
handleSubmitWithAction: (e?: React.BaseSyntheticEvent) => Promise<void>;
resetFormAndAction: () => void;
};

/**
* Infer the type of the return object of the `useHookFormAction` hook.
*/
export type InferUseHookFormActionHookReturn<T extends Function, FormContext = any> =
T extends SafeActionFn<
infer ServerError,
infer S extends Schema | undefined,
infer BAS extends readonly Schema[],
infer CVE,
infer CBAVE,
infer Data
>
? UseHookFormActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data, FormContext>
: never;

/**
* Infer the type of the return object of the `useHookFormOptimisticAction` hook.
*/
export type InferUseHookFormOptimisticActionHookReturn<T extends Function, State, FormContext = any> =
T extends SafeActionFn<
infer ServerError,
infer S extends Schema | undefined,
infer BAS extends readonly Schema[],
infer CVE,
infer CBAVE,
infer Data
>
? UseHookFormOptimisticActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data, State, FormContext>
: never;

0 comments on commit 08831e2

Please sign in to comment.