Skip to content
TypeScript logo

TypeScript

Officialv5.5.6

@kubb/plugin-ts

kubb add 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

OptionPurposeDefault
outputWhere files are written.{ path: 'types', barrel: { type: 'named' } }
groupSplit output by tag or path.None
enumHow enums are generated and named.{ type: 'asConst', constCasing: 'camelCase', typeSuffix: 'Key', keyCasing: 'none' }
↳ enum.typeRepresentation of each enum.'asConst'
↳ enum.constCasingCasing of the as const object.'camelCase'
↳ enum.typeSuffixSuffix of the companion type.'Key'
↳ enum.keyCasingCasing of enum keys.'none'
syntaxTypeType aliases or interfaces for object schemas.'type'
optionalTypeHow optional properties are written.'questionToken'
arrayTypeType[] or Array<Type>.'array'
operationsPer-operation type files, or inline types in consumers.'export'
printerReplace the handler for a schema type.None
includeKeep only operations and schemas that match.None
excludeSkip operations and schemas that match.[]
overrideApply different options per pattern.[]
resolverCustomize generated names and file paths. See resolver members.resolverTs
macrosRewrite AST nodes before printing.[]

Option details

enum

How enums are generated and how their names are cased.

Type{ type?, constCasing?, typeSuffix?, keyCasing? }
Requiredfalse
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'
Requiredfalse
Default'asConst'
'asConst'
Writes an as const object and a union type of its values. Bundlers drop the object when nothing uses it.
'enum'
Writes a TypeScript enum, which emits JavaScript runtime code.
'constEnum'
Writes a 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'
Writes a named union type with no runtime value.
'inlineLiteral'
Inlines the '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]

enum.constCasing

Casing of the const object when type is 'asConst'.

Type'camelCase' | 'pascalCase'
Requiredfalse
Default'camelCase'
'camelCase'
Names the const petStatus.
'pascalCase'
Names the const 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.

Typestring
Requiredfalse
Default'Key'
typeSuffix: 'Value'
export type PetStatusValue = (typeof petStatus)[keyof typeof petStatus]

enum.keyCasing

Casing of enum key names.

Type'none' | 'camelCase' | 'pascalCase' | 'snakeCase' | 'screamingSnakeCase'
Requiredfalse
Default'none'

For the enum value enum value, each casing writes this key:

'none'
Keeps the raw value from the spec.
'camelCase'
Writes enumValue.
'pascalCase'
Writes EnumValue.
'snakeCase'
Writes enum_value.
'screamingSnakeCase'
Writes ENUM_VALUE.

syntaxType

How object schemas are declared.

Type'type' | 'interface'
Requiredfalse
Default'type'
'type'
Writes export type Pet = { name: string }.
'interface'
Writes export interface Pet { name: string }. Use it when consumers need declaration merging.

optionalType

How optional properties are written.

Type'questionToken' | 'undefined' | 'questionTokenAndUndefined'
Requiredfalse
Default'questionToken'
'questionToken'
Writes type?: string. The property may be missing.
'undefined'
Writes type: string | undefined. The property must exist but may be undefined.
'questionTokenAndUndefined'
Writes type?: string | undefined. The property may be missing or undefined. Use it with "exactOptionalPropertyTypes": true.

arrayType

Syntax for array types.

Type'array' | 'generic'
Requiredfalse
Default'array'
'array'
Writes tags: string[].
'generic'
Writes 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'
Requiredfalse
Default'export'
'export'
Writes one file per operation with its aliases and aggregates, such as export type AddPetStatus200 = Pet.
'inline'
Writes only the schema types. Plugins that use them, such as clients and MSW handlers, print the operation types inline, such as 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 }
Requiredfalse
Returning a TypeScript compiler node is deprecated. See Map schema types for an example of a string handler.

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:

Partial override
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'
  }
}