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)
},
},
})export type Pet = { name: string }export type ApiPet = { name: string }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'
},
},
},
})export type Pet = {
birthday?: string
age?: number
}export type Pet = {
birthday?: Date
age?: bigint
}Each schema has its own handler:
| Schema | Handler |
|---|---|
format: date | date |
format: date-time | datetime |
integer, format: int32 or no format | integer |
integer, format: int64 | bigint, 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] })export type Pet = {
/**
* @description The name of the pet
* @type string
*/
name: string
}export type Pet = {
name: string
}The OpenAPI document stays unchanged. The same macro works with pluginZod, with one exception.
