SafeFn

Typescript

SafeFn exposes a few type that could be useful when working with it.

Action Result

These are the types related to the result of calling an action created with .createAction() directly from the client. When using the useServerAction hook, this is automatically converted into NeverThrow's types.

ActionErr

Used for constructing an action error. Takes in the error type.

type MyActionErr = ActionErr<{
  code: "DB_ERROR";
  cause: string;
}>;
 
const myActionErr: MyActionErr = {
  ok: false,
  error: {
    code: "DB_ERROR";
    cause: "Database connection failed";
  };
};

ActionOk

Used for constructing an action ok. Takes in the data type.

type MyActionOk = ActionOk<{
  name: string;
  age: number;
}>;
 
const myActionOk: MyActionOk = {
  ok: true,
  value: {
    name: "John",
    age: 20,
  },
};

ActionResult

Used for creating an action result. Takes in the possible Ok and Err types.

type MyActionResult = ActionResult<MyActionOk, MyActionErr>;

SafeFn

InferSafeFnActionArgsArgs

Infers the arguments that need to be passed to the function returned by createAction().

const myAction = createSafeFn()
  .input(z.object({ name: z.string() }))
  .handler(...)
  .createAction();
type MyActionArgs = InferSafeFnActionArgs<typeof myAction>;
//   ^ {name: string}

InferSafeFnActionReturn

Infers the return type of an action when calling it directly. When using the useServerAction hook, this is automatically converted into NeverThrow's types.

const myAction = createSafeFn()
  .handler(() => "Yay!" as const)
  .catch(() => "ooh no" as const);
type MyActionReturn = InferSafeFnActionReturn<typeof myAction>;
//   ^ ActionResult<"Yay!", "ooh no">

Infers the return type of an action.

InferSafeFnActionReturnData

Infers the ok type of an action.

const myAction = createSafeFn()
  .handler(() => ok("Yay!" as const))
  .createAction();
type MyActionOk = InferSafeFnActionOkData<typeof myAction>;
//   ^ "Yay!"

InferSafeFnActionReturnError

Infers the error type of an action.

const myAction = createSafeFn()
  .handler(...)
  .catch(() => err("ooh no" as const))
  .createAction();
type MyActionError = InferSafeFnActionReturnError<typeof myAction>;
//   ^ "ooh no"

InferSafeFnArgs

Infers the arguments that need to be passed into .run()

const mySafeFn = createSafeFn()
  .input(z.object({ name: z.string() }))
  .handler(...)
type MySafeFnArgs = InferSafeFnArgs<typeof mySafeFn>;
//   ^ {name: string}

InferSafeFnReturn

Infers the return type of a SafeFn when using .run(). This is the ResultAsync type, this can be converted into a Result type by wrapping it in Await

const mySafeFn = createSafeFn()
  .handler(() => "Yay!" as const)
  .catch(() => "ooh no" as const);
type MySafeFnReturn = InferSafeFnReturn<typeof mySafeFn, false>;
//   ^ ResultAsync<"Yay!", "ooh no">
type MyAwaitedSafeFnReturn = Awaited<MySafeFnReturn>;
//   ^ Result<"Yay!", "ooh no">

InferSafeFnReturnData

Infers the ok type of a SafeFn when using .run().

const mySafeFn = createSafeFn().handler(() => ok("Yay!" as const));
type MySafeFnOk = InferSafeFnReturnData<typeof mySafeFn>;
//   ^ "Yay!"

InferSafeFnReturnError

Infers the error type of a SafeFn when using .run().

const mySafeFn = createSafeFn()
  .handler(...)
  .catch(() => "ooh no" as const);
type MySafeFnError = InferSafeFnReturnError<typeof mySafeFn>;
//   ^ "ooh no"

On this page