Skip to content
Parser
TypeScript logo
parsers: [parserTs()]
Officialv5.21.2kubb >=5.8.3node >=22.22

TypeScript parser

@kubb/parser-ts

Prints the Kubb AST as TypeScript source and rewrites the extensions of relative imports and exports.

kubb add parser-ts
typescripttsxparserprinterast

The TypeScript parser prints the .ts and .js files that plugins generate, with imports, exports, declarations, and JSDoc.

  • It rewrites relative import paths.
  • The package also exports parserTsx for .tsx and .jsx files, with the same options.

Installation

kubb add parser-ts

kubb add installs the version the Kubb registry pins and adds parserTs() to kubb.config.ts. It needs a kubb.config.ts, so run kubb init first in a new project.

Setup

No setup is needed. defineConfig and the bundler integrations register parserTs(), parserTsx(), and parserMd() by default. Set parsers only to change an option or to add a parser.

A custom parsers array replaces the default set, so list every parser your plugins need. A file that no parser claims is written as its source text only, without imports, exports, banner, or footer.

Options worth knowing

OptionDefaultWhat it does
extension{ '.ts': '' }Sets the extension that relative imports and exports get. Use { '.ts': '.js' } for ESM output and { '.ts': '.ts' } to run .ts files directly in Node.js.

parserTsx reads its own extension map, so set the .tsx key on it too.

Example

This config adds .js to relative imports in .ts and .tsx files and keeps every default parser.

import { defineConfig } from 'kubb/config'
import { parserMd } from '@kubb/parser-md'
import { parserTs, parserTsx } from '@kubb/parser-ts'
import { pluginTs } from '@kubb/plugin-ts'

export default defineConfig({
  input: './petStore.yaml',
  output: { path: './src/gen' },
  parsers: [
    parserTs({ extension: { '.ts': '.js' } }),
    parserTsx({ extension: { '.tsx': '.js' } }),
    parserMd(),
  ],
  plugins: [pluginTs()],
})

Dependencies

No extension dependencies.