Skip to Content
API ReferenceApiTypes<T>

ApiTypes<TApi>

Extracts the inferred request and response types from a fully typed API client.

import type { ApiTypes } from '@routar/core' type TodoApiTypes = ApiTypes<typeof todoApi> type CreateRequest = TodoApiTypes['create']['request'] // { body: { title: string } } type CreateResponse = TodoApiTypes['create']['response'] // Todo type ListResponse = TodoApiTypes['getList']['response'] // Todo[]

Why use it

When you need to reference the input or output types of an API call — in form handlers, server actions, state types, or function signatures — ApiTypes derives them from the single source of truth instead of duplicating type definitions:

// ❌ duplicating the type manually type CreateTodoInput = { body: { title: string } } // ✅ derived from the API client type CreateTodoInput = ApiTypes<typeof todoApi>['create']['request']

Type shape

The resulting type is an object keyed by endpoint name, each with request and response fields:

type TodoApiTypes = ApiTypes<typeof todoApi> // { // getList: { request: {}; response: Todo[] } // getDetail: { request: { path: { id: number } }; response: Todo } // create: { request: { body: { title: string } }; response: Todo } // }

With nested routers

For nested routers, index through the client shape:

const api = createApi(executor, apiRouter) type ApiT = ApiTypes<typeof api> type UserDetailResponse = ApiT['users']['getDetail']['response'] type UserTodosResponse = ApiT['users']['todos']['getList']['response']

Exporting types from an API module

The standard pattern is to export ApiTypes alongside the API client so consumers can reference the types without re-importing @routar/core:

// todo.api.ts export const todoApi = createApi(executor, todoRouter) export type TodoApiTypes = ApiTypes<typeof todoApi> export type TodoItem = TodoApiTypes['getDetail']['response']
Last updated on