Skip to content
rest-rpc
Esc
navigateopen⌘Jpreview
On this page

Type Helpers

Name inferred route, client, and handler types when you need them.

Most rest-rpc types are inferred through normal usage.

Use helper types when your application needs to name inferred request, response, handler, client, TanStack Query, or React Query types.

This page covers common helper categories. The exact export surface should be checked during the v1 API audit.

Client Helpers

Client helper types live in @rest-rpc/core.

import type {
	ApiClientFor,
	ClientRequest,
	ClientResponse,
	ClientSuccessBody,
} from "@rest-rpc/core";
import { api } from "./contract";

type GetTodoRequest = ClientRequest<typeof api.todos.get>;
type GetTodoResponse = ClientResponse<typeof api.todos.get>;
type GetTodoBody = ClientSuccessBody<typeof api.todos.get>;

Use these when wrapping the generated client in application-specific functions.

async function loadTodo(
	client: ApiClientFor<typeof api>,
	input: GetTodoRequest,
): Promise<GetTodoBody> {
	return client.todos.get.fetch(input);
}

Server Helpers

Server helper types are split between core route types and adapter-specific handler types.

import type { RouteHandler } from "@rest-rpc/express";
import { api } from "./contract";

type GetTodoHandler = RouteHandler<typeof api.todos.get>;

Use adapter package helpers when the type depends on adapter context.

import type { RouteRequest } from "@rest-rpc/hono";

type GetTodoHandlerInput = RouteRequest<typeof api.todos.get>;

Response Helpers

Response helpers can extract success and error shapes.

import type {
	ClientErrors,
	ClientSuccessResponse,
} from "@rest-rpc/core";

type GetTodoSuccess = ClientSuccessResponse<typeof api.todos.get>;
type GetTodoErrors = ClientErrors<typeof api.todos.get>;

Use these when app code wants to handle a route response outside the generated client call.

TanStack Query Helpers

TanStack Query helper types live in @rest-rpc/tanstack-query.

import type {
	RouteMutationVariables,
	RouteQueryData,
	RouteQueryError,
} from "@rest-rpc/tanstack-query";

type GetTodoData = RouteQueryData<typeof api.todos.get>;
type GetTodoError = RouteQueryError<typeof api.todos.get>;
type CreateTodoVariables = RouteMutationVariables<typeof api.todos.create>;

These are useful when extracting options, callbacks, component props, or local TanStack Query wrappers.

Keep Helpers Close To The Need

Prefer normal inference until a named type helps.

Good uses:

  • exported application service functions
  • component props
  • wrapper hooks
  • test fixtures
  • shared error handling helpers

Avoid adding named helper types when the generated client or handler already infers the type locally.

Was this page helpful?