Infer Types ZSA provides some helpful utility types that allow you to infer the input, return type, return data, and hot return type of a server action.
Use inferServerActionReturnData
to infer the return data type of a server action. This will be the type of the data
in the [data, error]
tuple.
import { inferServerActionReturnData } from "zsa"
const myAction = createServerAction ()
. input (z. object ({ num: z. number () }))
. output (z. number ())
. handler ( async ({ input }) => {
return input.num + 1
})
type MyActionReturnData = inferServerActionReturnData < typeof myAction>
// ^? number
Use inferServerActionError
to infer the error type of a server action. This will be the type of the error
in the [data, error]
tuple.
import { inferServerActionError } from "zsa"
const myAction = createServerAction ()
. input (z. object ({ num: z. number () }))
. output (z. number ())
. handler ( async ({ input }) => {
return input.num + 1
})
type MyActionError = inferServerActionError < typeof myAction>
// ^? TZSAError<z.ZodType<number>>
Use inferServerActionReturnType
to infer the return type of a server action. This will be a tuple of [data, error]
.
import { inferServerActionReturnType } from "zsa"
const myAction = createServerAction ()
. input (z. object ({ num: z. number () }))
. output (z. number ())
. handler ( async ({ input }) => {
return input.num + 1
})
type MyActionReturnType = inferServerActionReturnType < typeof myAction>
// ^? [number, null] | [null, TZSAError]
Use inferServerActionReturnTypeHot
to infer the hot (unresolved) return type of a server action. This will be a tuple promise.
import { inferServerActionReturnTypeHot } from "zsa"
const myAction = createServerAction ()
. input (z. object ({ num: z. number () }))
. output (z. number ())
. handler ( async ({ input }) => {
return input.num + 1
})
type MyActionReturnTypeHot = inferServerActionReturnTypeHot < typeof myAction>
// ^? Promise<[number, null] | [null, TZSAError]>
Use inferServerActionInput
to infer the input type of a server action.
import { inferServerActionInput } from "zsa"
const myAction = createServerAction ()
. input (z. object ({ num: z. number () }))
. handler ( async ({ input }) => {
return input.num + 1
})
type MyActionInput = inferServerActionInput < typeof myAction>
// ^? { num: number }
These utility types make it easy to work with the inputs, outputs, and intermediate types of your server actions in a type-safe way without having to manually specify the types. They cover inferring the input type, return tuple type, just the success return data type, and the hot (promise) return type.