Skip to content
TypeScript logo

TypeScript

Officialv5.5.6

@kubb/plugin-ts

kubb add plugin-ts

Customize generated types

Prefix TypeScript names, change how a schema type prints, and remove descriptions.

Add each change to pluginTs() in the TypeScript plugin configuration.

Prefix type names

Override resolver.name and call resolverTs.name first, so the plugin's PascalCase rule still applies.

import { pluginTs, resolverTs } from '@kubb/plugin-ts'

pluginTs({
  resolver: {
    name(name) {
      return 'Api' + resolverTs.name(name)
    },
  },
})

File names stay the same unless you also override resolver.file. See every resolver member.

Map schema types

Replace a printer handler. Each handler returns the TypeScript type as a string.

import { pluginTs } from '@kubb/plugin-ts'

pluginTs({
  printer: {
    nodes: {
      date() {
        return 'Date'
      },
      integer() {
        return 'bigint'
      },
    },
  },
})

Each schema has its own handler:

SchemaHandler
format: datedate
format: date-timedatetime
integer, format: int32 or no formatinteger
integer, format: int64bigint, already bigint by default
A handler that returns a TypeScript compiler node is deprecated and is removed in the next major. Until then, it needs @kubb/parser-ts, plus typescript 5 or 6, or @typescript/typescript6 next to TypeScript 7. Without @kubb/parser-ts, the build stops with an error that tells you to install it.

Remove descriptions

Clear schema descriptions with a macro.

import { pluginTs } from '@kubb/plugin-ts'
import { defineMacro } from 'kubb/kit'

const dropDescriptions = defineMacro({
  name: 'drop-descriptions',
  schema(node) {
    return 'description' in node && node.description
      ? { ...node, description: undefined }
      : undefined
  },
})

pluginTs({ macros: [dropDescriptions] })

The OpenAPI document stays unchanged. The same macro works with pluginZod, with one exception.