Skip to content

operationTypes to simplify generated operation types #16

Description

@alario-tang

operationTypes — simplify generated operation types

Add an operationTypes?: boolean option (default true) to @kubb/plugin-ts that references base schema types directly instead of the per-operation alias layer.

Behavior

With operationTypes: false, a request body or response backed by a single $ref resolves to the referenced component type (e.g. Pet) instead of the AddPetData / AddPetStatus200 alias. Inline, array, and union schemas keep their per-operation alias because no single base type exists.

  • @kubb/plugin-ts stops emitting the redundant $ref-backed aliases and references the base component inside the aggregate XxxResponses / XxxResponse types.
  • The option lives on @kubb/plugin-ts only. It makes the TypeScript resolver inline $ref-backed names; consumer plugins (@kubb/plugin-client, @kubb/plugin-react-query, @kubb/plugin-vue-query) read that resolver through the plugin driver, so they inherit the inlining automatically — no per-plugin flag to keep in sync.
  • Default (true) keeps existing output unchanged.
export default defineConfig({
  input: { path: './petStore.yaml' },
  output: { path: './src/gen' },
  plugins: [
    pluginTs({ operationTypes: false }),
    pluginClient(),
  ],
})

Before (operationTypes: true, default)

import type { AddPetData, AddPetStatus200, AddPetStatus422 } from '../models/AddPet'

export async function addPet(data?: AddPetData, config: Partial<RequestConfig<AddPetData>> = {}) {
  const res = await request<AddPetStatus200, ResponseErrorConfig<AddPetStatus422>, AddPetData>({ /* ... */ })
  return res.data
}

After (operationTypes: false)

import type { Pet } from '../models/Pet'
import type { HttpValidationError } from '../models/HttpValidationError'

export async function addPet(data?: Pet, config: Partial<RequestConfig<Pet>> = {}) {
  const res = await request<Pet, ResponseErrorConfig<HttpValidationError>, Pet>({ /* ... */ })
  return res.data
}

Note on the 2xx discussion

The separate request from the thread — narrowing the success/return type (and TanStack TData) to the 2xx response instead of the union of all status codes — already shipped and is unchanged by this option. operationTypes only controls whether the names are the base component types or the per-operation aliases.

Implemented in #222.

Original request (by @alario-tang)

Feature Request: Option to simplify generated operation types

Problem

When generating TypeScript types for API operations, Kubb creates multiple type aliases that are often redundant:

// Generated in models/orderManagement/OrderManagementGetAllPortfolioTrades.ts

export type OrderManagementGetAllPortfolioTrades200 = PortfolioTradesResponseCO;
export type OrderManagementGetAllPortfolioTrades422 = HTTPValidationError;
export type OrderManagementGetAllPortfolioTradesMutationResponse = OrderManagementGetAllPortfolioTrades200;
export type OrderManagementGetAllPortfolioTradesMutation = {
  Response: OrderManagementGetAllPortfolioTrades200;
  Request: OrderManagementGetAllPortfolioTradesMutationRequest;
  Errors: OrderManagementGetAllPortfolioTrades422;
};

In practice, I only need to import the base business type (PortfolioTradesResponseCO) directly. The operation-level aliases add noise and increase bundle size.

Proposed Solution

Add a configuration option like operationTypes: false to skip generating operation-level type aliases and use the base types directly in generated functions.

Use Case

  • Projects that don't need fine-grained error type handling per status code
  • Cleaner imports in business code
  • Smaller generated output

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Fields

    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions