Convert OpenAPI 3.0 & 3.1 schemas to TypeScript type definitions
npx @tessl/cli install tessl/npm-openapi-typescript@7.9.0OpenAPI TypeScript is a comprehensive TypeScript code generation tool that converts OpenAPI 3.0 and 3.1 schemas into accurate TypeScript type definitions. It provides both a programmatic API and CLI interface, enabling developers to automatically generate type-safe TypeScript interfaces from their OpenAPI specifications with extensive customization options and advanced features.
npm install openapi-typescriptimport openapiTS, { COMMENT_HEADER } from "openapi-typescript";For CommonJS:
const openapiTS = require("openapi-typescript");Transform functions:
import {
transformSchema,
transformSchemaObject,
transformComponentsObject,
transformPathsObject,
transformOperationObject,
astToString
} from "openapi-typescript";# Generate types from local file
openapi-typescript ./api.yaml --output ./types.ts
# Generate types from URL
openapi-typescript https://api.example.com/openapi.json -o ./api-types.ts
# With advanced options
openapi-typescript ./api.yaml \
--output ./types.ts \
--export-type \
--immutable \
--enumimport openapiTS, { astToString } from "openapi-typescript";
import fs from "node:fs";
// Convert schema to TypeScript AST
const ast = await openapiTS("./api.yaml", {
exportType: true,
immutable: true,
enum: true
});
// Convert AST to TypeScript source code
const output = astToString(ast);
// Write to file
fs.writeFileSync("./types.ts", output);OpenAPI TypeScript is built around several key components:
openapiTS() function that orchestrates the conversion processThe main conversion functionality that transforms OpenAPI schemas into TypeScript AST nodes. Supports all input formats and provides extensive customization options.
function openapiTS(
source: string | URL | OpenAPI3 | Buffer | Readable,
options?: OpenAPITSOptions
): Promise<ts.Node[]>;
interface OpenAPITSOptions {
additionalProperties?: boolean;
alphabetize?: boolean;
arrayLength?: boolean;
defaultNonNullable?: boolean;
emptyObjectsUnknown?: boolean;
enum?: boolean;
enumValues?: boolean;
dedupeEnums?: boolean;
excludeDeprecated?: boolean;
exportType?: boolean;
immutable?: boolean;
rootTypes?: boolean;
rootTypesNoSchemaPrefix?: boolean;
pathParamsAsTypes?: boolean;
propertiesRequiredByDefault?: boolean;
inject?: string;
transform?: (schemaObject: SchemaObject, metadata: TransformNodeOptions) => ts.TypeNode;
postTransform?: (ast: ts.Node[]) => ts.Node[];
makePathsEnum?: boolean;
generatePathParams?: boolean;
silent?: boolean;
cwd?: string | URL;
redocly?: Config;
}
const COMMENT_HEADER: string;Transform functions for converting individual OpenAPI objects to TypeScript AST nodes. Essential for custom processing and advanced integrations.
function transformSchema(schema: OpenAPI3, ctx: GlobalContext): ts.Node[];
function transformSchemaObject(
schemaObject: SchemaObject | ReferenceObject,
options: TransformNodeOptions
): ts.TypeNode;
function transformComponentsObject(
componentsObject: ComponentsObject,
ctx: GlobalContext
): ts.Node[];
function transformPathsObject(
pathsObject: PathsObject,
ctx: GlobalContext
): ts.TypeNode;Comprehensive utilities for creating and manipulating TypeScript AST nodes. Provides building blocks for custom transform functions and AST manipulation.
function astToString(
ast: ts.Node[],
options?: AstToStringOptions
): string;
function stringToAST(source: string): unknown[];
function tsLiteral(value: unknown): ts.TypeNode;
function tsUnion(types: ts.TypeNode[]): ts.TypeNode;
function tsIntersection(types: ts.TypeNode[]): ts.TypeNode;
interface AstToStringOptions {
formatOptions?: FormatCodeSettings;
banner?: string;
footer?: string;
}Complete command-line interface with extensive configuration options for integration into build processes and development workflows.
// CLI Options Interface (conceptual representation)
interface CLIOptions {
help?: boolean;
version?: boolean;
output?: string;
redocly?: string;
check?: boolean;
exportType?: boolean;
immutable?: boolean;
enum?: boolean;
enumValues?: boolean;
dedupeEnums?: boolean;
additionalProperties?: boolean;
emptyObjectsUnknown?: boolean;
defaultNonNullable?: boolean;
propertiesRequiredByDefault?: boolean;
arrayLength?: boolean;
pathParamsAsTypes?: boolean;
alphabetize?: boolean;
excludeDeprecated?: boolean;
rootTypes?: boolean;
rootTypesNoSchemaPrefix?: boolean;
makePathsEnum?: boolean;
}Core TypeScript type definitions for OpenAPI specification objects and configuration options.
interface OpenAPI3 {
openapi: string;
info: InfoObject;
jsonSchemaDialect?: string;
servers?: ServerObject[];
paths?: PathsObject;
webhooks?: { [id: string]: PathItemObject | ReferenceObject };
components?: ComponentsObject;
security?: SecurityRequirementObject[];
tags?: TagObject[];
externalDocs?: ExternalDocumentationObject;
$defs?: $defs;
}
interface GlobalContext {
additionalProperties: boolean;
alphabetize: boolean;
arrayLength: boolean;
defaultNonNullable: boolean;
discriminators: Map<string, DiscriminatorObject>;
emptyObjectsUnknown: boolean;
enum: boolean;
enumValues: boolean;
dedupeEnums: boolean;
excludeDeprecated: boolean;
exportType: boolean;
immutable: boolean;
rootTypes: boolean;
rootTypesNoSchemaPrefix: boolean;
pathParamsAsTypes: boolean;
propertiesRequiredByDefault: boolean;
makePathsEnum: boolean;
generatePathParams: boolean;
silent: boolean;
resolve(ref: string): any;
}
interface TransformNodeOptions {
path: string;
ctx: GlobalContext;
schema: OpenAPI3;
}