---
url: /plugins/plugin-ts/recipes/tree-shakeable-enums.md
description: >-
  Emit each OpenAPI enum as an as const object so bundlers drop the values you
  never use.
---

# Tree-shakeable enums

Set [`enum.type`](/plugins/plugin-ts/reference/options#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](/plugins/plugin-ts/reference/options#enum-type) for the other representations `enum.type` can produce.

```typescript [kubb.config.ts]
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

```typescript [src/gen/types/Pet.ts]
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];
```

```typescript [usage.ts]
import { petStatusEnum, type PetStatusEnumKey } from './src/gen/types/Pet'

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