Skip to content

Map spec types to native TS

Use printer.nodes to replace the handler for a schema type with one that builds its own TypeScript AST node. This config maps the date type to a Date type reference and integer to the bigint keyword.

kubb.config.ts
typescript
import { defineConfig } from 'kubb/config'
import ts from 'typescript'
import { pluginTs } from '@kubb/plugin-ts'

export default defineConfig({
  input: './petStore.yaml',
  output: { path: './src/gen', clean: true },
  plugins: [
    pluginTs({
      output: { path: 'types', mode: 'directory' },
      printer: {
        nodes: {
          date() {
            return ts.factory.createTypeReferenceNode('Date', [])
          },
          integer() {
            return ts.factory.createKeywordTypeNode(ts.SyntaxKind.BigIntKeyword)
          },
        },
      },
    }),
  ],
})

Output example

The petStore fixture has no plain date (only date-time) schemas. format: int64 fields like id and petId are already bigint by default (the adapter's integerType option defaults to 'bigint'), so the override has no effect on them. It does affect format: int32 fields such as quantity, which becomes bigint only because of the integer handler override (the same printer mechanism the recipe uses for date):

src/gen/types/Order.ts
typescript
export type Order = {
    id?: bigint;
    petId?: bigint;
    quantity?: bigint;
    // ...
};
usage.ts
typescript
import type { Order } from './src/gen/types/Order'

function describe(order: Order) {
  // id is bigint, not number, so it needs the `n` suffix or BigInt()
  return order.id === 10n
}