Skip to content

operationTypes to simplify generated operation types #16

@alario-tang

Description

@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

// Alias 1: Status code specific
export type OrderManagementGetAllPortfolioTrades200 = PortfolioTradesResponseCO;

// Alias 2: Error type
export type OrderManagementGetAllPortfolioTrades422 = HTTPValidationError;

// Alias 3: Another alias of alias
export type OrderManagementGetAllPortfolioTradesMutationResponse = OrderManagementGetAllPortfolioTrades200;

// Alias 4: Composite type
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 simplifiedTypes or operationTypes: false to skip generating operation-level type aliases:

pluginTs({
output: { path: './models' },
operationTypes: false, // Skip OrderManagementGetAllPortfolioTrades200, etc.
}),

pluginClient({
output: { path: './api' },
operationTypes: false, // Use base types directly in generated functions
}),

Expected Behavior

With operationTypes: false, the generated client function would directly use the base types:

// Before
import type {
OrderManagementGetAllPortfolioTradesMutationRequest,
OrderManagementGetAllPortfolioTradesMutationResponse,
OrderManagementGetAllPortfolioTrades422,
} from "../../models/...";

export async function orderManagementGetAllPortfolioTrades(
data: OrderManagementGetAllPortfolioTradesMutationRequest,
...
): Promise

// After (simplified)
import type {
PortfolioTradesQueryCO,
PortfolioTradesResponseCO,
} from "../../models";

export async function orderManagementGetAllPortfolioTrades(
data: PortfolioTradesQueryCO,
...
): Promise

Use Case

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

Alternatives Considered

  1. Import base types manually - Works but defeats the purpose of generated types
  2. Post-processing script - Adds complexity to the build pipeline

Would love to hear if this aligns with Kubb's design philosophy or if there's an existing way to achieve this. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions