TypeScript Parser
The typescriptParser formats TypeScript (.ts) files, organizing imports, exports, and source code with consistent formatting.
Use typescriptParser when: Generating TypeScript type definitions, interfaces, constants, or functions.
Perfect for: Type generators, API client generation, SDK TypeScript files, config generators.
Key features:
- Formats TypeScript code
- Organizes imports and exports
- Extension mapping support
- Automatic parser selection
Installation
The typescriptParser is included in @kubb/fabric-core.
Import:
import { typescriptParser } from '@kubb/fabric-core/parsers'Usage
Register the TypeScript parser to format .ts files.
Example: Parse TypeScript files with the parser.
import { createFabric } from '@kubb/fabric-core'
import { typescriptParser } from '@kubb/fabric-core/parsers'
const fabric = createFabric()
fabric.use(typescriptParser)Parser Options
Options are passed during file parsing via fabric.write(), not during parser registration.
file
File that will be parsed.
| Type: | KubbFile.File |
|---|---|
| Required: | true |
extname
Extension to use when emitting import/export paths.
TIP
Use this to rewrite import paths, for example from ./file to ./file.ts.
| Type: | string |
|---|---|
| Required: | false |
| Default: | '.ts' |
Example:
// Parser rewrites imports to include .ts extension
await fabric.write({
extension: { '.ts': '.ts' },
})
// Imports become: import { x } from './module.ts'Parser Selection
The typescriptParser is automatically selected for files with .ts extension when using fabric.write() with extension mapping.
import { createFabric } from '@kubb/fabric-core'
import { fsPlugin } from '@kubb/fabric-core/plugins'
import { typescriptParser } from '@kubb/fabric-core/parsers'
const fabric = createFabric()
fabric.use(fsPlugin)
fabric.use(typescriptParser)
await fabric.addFile({
baseName: 'types.ts',
path: './output/types.ts',
sources: [
{ value: 'export type User = { id: number }', isExportable: true },
],
imports: [],
exports: []
})
// typescriptParser is used for .ts files
await fabric.write({
extension: { '.ts': '.ts' },
})Features
Import Formatting
Formats TypeScript imports with proper syntax:
await fabric.addFile({
baseName: 'api.ts',
path: './output/api.ts',
imports: [
{ name: 'User', path: './types', isTypeOnly: true },
{ name: 'axios', path: 'axios' },
],
sources: [
{ value: 'export const fetchUser = async (id: number): Promise<User> => {}', isExportable: true },
],
exports: []
})Generates:
import type { User } from './types'
import axios from 'axios'
export const fetchUser = async (id: number): Promise<User> => {}Export Formatting
Formats TypeScript exports:
await fabric.addFile({
baseName: 'index.ts',
path: './output/index.ts',
exports: [
{ name: 'User', path: './types', isTypeOnly: true },
{ name: 'fetchUser', path: './api' },
],
imports: []
})Generates:
export type { User } from './types'
export { fetchUser } from './api'Source Code
Handles TypeScript source code formatting:
await fabric.addFile({
baseName: 'types.ts',
path: './output/types.ts',
sources: [
{
value: 'export type User = { id: number; name: string; email: string }',
isExportable: true
},
{
value: 'export type Post = { id: number; title: string; userId: number }',
isExportable: true
},
],
imports: [],
exports: [],
})Generates:
export type User = { id: number; name: string; email: string }
export type Post = { id: number; title: string; userId: number }Examples
Basic TypeScript File
import { createFabric } from '@kubb/fabric-core'
import { fsPlugin } from '@kubb/fabric-core/plugins'
import { typescriptParser } from '@kubb/fabric-core/parsers'
const fabric = createFabric()
fabric.use(fsPlugin)
fabric.use(typescriptParser)
await fabric.addFile({
baseName: 'user.ts',
path: './output/user.ts',
sources: [
{ value: 'export type User = { id: number; name: string }', isExportable: true },
{ value: 'export const defaultUser: User = { id: 0, name: "Guest" }', isExportable: true },
],
imports: [],
exports: [],
})
await fabric.write({ extension: { '.ts': '.ts' } })With Imports
import { createFabric } from '@kubb/fabric-core'
import { fsPlugin } from '@kubb/fabric-core/plugins'
import { typescriptParser } from '@kubb/fabric-core/parsers'
const fabric = createFabric()
fabric.use(fsPlugin)
fabric.use(typescriptParser)
await fabric.addFile({
baseName: 'api.ts',
path: './output/api.ts',
imports: [
{ name: 'User', path: './types', isTypeOnly: true },
{ name: 'axios', path: 'axios' },
],
exports: [],
sources: [
{
value: 'export const getUser = (id: number): Promise<User> => axios.get(`/users/${id}`)',
isExportable: true
},
],
})
await fabric.write({ extension: { '.ts': '.ts' } })Multiple Files
import { createFabric } from '@kubb/fabric-core'
import { fsPlugin } from '@kubb/fabric-core/plugins'
import { typescriptParser } from '@kubb/fabric-core/parsers'
const fabric = createFabric()
fabric.use(fsPlugin, { clean: { path: './output' } })
fabric.use(typescriptParser)
// Types file
await fabric.addFile({
baseName: 'types.ts',
path: './output/types.ts',
sources: [
{ value: 'export type User = { id: number; name: string }', isExportable: true },
],
imports: [],
exports: [],
})
// API file with imports
await fabric.addFile({
baseName: 'api.ts',
path: './output/api.ts',
imports: [
{ name: 'User', path: './types', isTypeOnly: true },
],
sources: [
{ value: 'export const fetchUser = async (): Promise<User> => {}', isExportable: true },
],
exports: [],
})
await fabric.write({ extension: { '.ts': '.ts' } })Extension Names
The typescriptParser handles the following extensions:
.ts
For TypeScript files with JSX, use the tsxParser.
See Also
- tsxParser — Parse TSX files with JSX
- defaultParser — Fallback parser
- Creating Parsers — Create custom parsers
- fsPlugin — Write files to disk
