TypeScript
Officialv5.5.4@kubb/plugin-ts
Options
Pass these options to pluginTs(). Shared options link to Shared plugin options, and this page gives only their default here.
Options overview
| Option | Purpose | Default |
|---|---|---|
output | Where files are written. | { path: 'types', barrel: { type: 'named' } } |
group | Split output by tag or path. | None |
enum | How enums are generated and named. | { type: 'asConst', constCasing: 'camelCase', typeSuffix: 'Key', keyCasing: 'none' } |
↳ enum.type | Representation of each enum. | 'asConst' |
↳ enum.constCasing | Casing of the as const object. | 'camelCase' |
↳ enum.typeSuffix | Suffix of the companion type. | 'Key' |
↳ enum.keyCasing | Casing of enum keys. | 'none' |
syntaxType | Type aliases or interfaces for object schemas. | 'type' |
optionalType | How optional properties are written. | 'questionToken' |
arrayType | Type[] or Array<Type>. | 'array' |
operations | Per-operation type files, or inline types in consumers. | 'export' |
printer | Replace the handler for a schema type. | None |
include | Keep only operations and schemas that match. | None |
exclude | Skip operations and schemas that match. | [] |
override | Apply different options per pattern. | [] |
resolver | Customize generated names and file paths. See resolver members. | resolverTs |
macros | Rewrite AST nodes before printing. | [] |
Option details
enum
How enums are generated and how their names are cased.
| Type | { type?, constCasing?, typeSuffix?, keyCasing? } |
| Required | false |
| Default | { type: 'asConst', constCasing: 'camelCase', typeSuffix: 'Key', keyCasing: 'none' } |
constCasing and typeSuffix apply only to 'asConst'. keyCasing also applies to 'enum' and 'constEnum'.
enum.type
Representation of each enum.
| Type | 'asConst' | 'enum' | 'constEnum' | 'literal' | 'inlineLiteral' |
| Required | false |
| Default | 'asConst' |
as const object and a union type of its values. Bundlers drop the object when nothing uses it.enum, which emits JavaScript runtime code.const enum, inlined at compile time. With --isolatedModules, code cannot use it through a .d.ts file, such as the types of a published package.'literal' union of an enum declared on a property. A component enum reached through a $ref keeps its named type.export const petStatus = {
available: 'available',
pending: 'pending',
sold: 'sold',
} as const
export type PetStatusKey = (typeof petStatus)[keyof typeof petStatus]export enum PetStatus {
available = 'available',
pending = 'pending',
sold = 'sold',
}export type PetStatus = 'available' | 'pending' | 'sold'enum.constCasing
Casing of the const object when type is 'asConst'.
| Type | 'camelCase' | 'pascalCase' |
| Required | false |
| Default | 'camelCase' |
petStatus.PetStatus, the same as the schema.enum.typeSuffix
Suffix of the companion type when type is 'asConst'. The const name never changes. Set '' with constCasing: 'pascalCase' to give the const and the type one name, PetStatus.
| Type | string |
| Required | false |
| Default | 'Key' |
export type PetStatusValue = (typeof petStatus)[keyof typeof petStatus]enum.keyCasing
Casing of enum key names.
| Type | 'none' | 'camelCase' | 'pascalCase' | 'snakeCase' | 'screamingSnakeCase' |
| Required | false |
| Default | 'none' |
For the enum value enum value, each casing writes this key:
enumValue.EnumValue.enum_value.ENUM_VALUE.syntaxType
How object schemas are declared.
| Type | 'type' | 'interface' |
| Required | false |
| Default | 'type' |
export type Pet = { name: string }.export interface Pet { name: string }. Use it when consumers need declaration merging.optionalType
How optional properties are written.
| Type | 'questionToken' | 'undefined' | 'questionTokenAndUndefined' |
| Required | false |
| Default | 'questionToken' |
type?: string. The property may be missing.type: string | undefined. The property must exist but may be undefined.type?: string | undefined. The property may be missing or undefined. Use it with "exactOptionalPropertyTypes": true.arrayType
Syntax for array types.
| Type | 'array' | 'generic' |
| Required | false |
| Default | 'array' |
tags: string[].tags: Array<string>, which reads better for complex elements.operations
Whether each operation gets its own types file, with types such as AddPetOptions, AddPetResponses, and AddPetStatus200.
| Type | 'export' | 'inline' |
| Required | false |
| Default | 'export' |
export type AddPetStatus200 = Pet.Options<{ body: Order; path?: never; query?: never; headers?: never }>. An enum declared inside an operation becomes a literal union. Needs a Kubb version with inline sources.printer
Replaces the handler for a schema type such as 'integer' or 'date'. Each handler returns the TypeScript type as a string. Inside a handler, this.transform(node) prints a nested node, and this.options holds the printer options. See the printer guide.
| Type | { nodes?: PrinterTsNodes } |
| Required | false |
Resolver members
Members you leave out of a resolver patch keep resolverTs. Besides the shared name, file, and imports members of resolver, resolverTs has these:
type ResolverTsPatch = {
param?: {
name?(node: OperationNode, param: ParameterNode): string // → 'DeletePetPathPetId'
path?(node: OperationNode, param: ParameterNode): string // → 'GetPetByIdPath'
query?(node: OperationNode, param: ParameterNode): string // → 'FindPetsByStatusQuery'
headers?(node: OperationNode, param: ParameterNode): string // → 'DeletePetHeaders'
cookies?(node: OperationNode, param: ParameterNode): string // → 'GetCookiesCookies'
}
response?: {
status?(node: OperationNode, statusCode: StatusCode): string // → 'ListPetsStatus200'
headers?(node: OperationNode, statusCode: StatusCode): string // → 'ListPetsStatus200Headers'
options?(node: OperationNode): string // → 'ListPetsOptions'
responses?(node: OperationNode): string // → 'ListPetsResponses'
response?(node: OperationNode): string // → 'ListPetsResponse'
body?(node: OperationNode): string // → 'CreatePetBody'
}
enum?: {
keyName?(node: { name?: string | null }, enumTypeSuffix?: string): string // → 'PetStatusKey'
}
}