Skip to content

Tree-shakeable enums

Set enum.type to 'asConst' to emit each enum as an as const object plus a companion key type. The object carries no runtime beyond its values, so bundlers drop what you do not use. See the option reference for the other representations enum.type can produce.

kubb.config.ts
typescript
import { defineConfig } from 'kubb/config'
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' },
      enum: { type: 'asConst' },
    }),
  ],
})

Output example

src/gen/types/Pet.ts
typescript
export const petTypeEnum = {
    dog: "dog",
    cat: "cat"
} as const;

export type PetTypeEnumKey = (typeof petTypeEnum)[keyof typeof petTypeEnum];

export const petStatusEnum = {
    available: "available",
    pending: "pending",
    sold: "sold"
} as const;

export type PetStatusEnumKey = (typeof petStatusEnum)[keyof typeof petStatusEnum];
usage.ts
typescript
import { petStatusEnum, type PetStatusEnumKey } from './src/gen/types/Pet'

function isAvailable(status: PetStatusEnumKey) {
  return status === petStatusEnum.available
}