Generate TypeScript types from OpenAPI 2 (Swagger) and OpenAPI 3.x specs. Loads from URL, file path, or object; resolves internal and external
$refs; outputs interfaces and type aliases.
Pass a URL, file path, or parsed spec; use output with any of them to write the result to a file (or directory when using split).
import { generateTypes } from "@pinta365/openapi-typegen";
// From URL (string)
const source = await generateTypes("https://api.example.com/openapi.json");
// From file path (string)
const fromFile = await generateTypes("references/openapi.json", { output: "api.d.ts" });
// From parsed spec object
const fromObject = await generateTypes({ openapi: "3.0.0", components: { schemas: { ... } } });Returns a Promise<string> of the generated TypeScript source. Optionally writes to a file (or directory when split is set) when output is set.
spec(SpecInput): URL string, file path string,URLinstance, or parsed OpenAPI/Swagger object (OpenAPI 2 or 3.x).options(GenerateOptions): Optional. See below.
Splitting output
When split is set, pass a directory path as output. Types are grouped into multiple files; shared types go to common.ts, and an index.ts
re-exports everything. Use split: "tag" to group by OpenAPI operation tags (e.g. one file per tag like “Daily Activity Routes”). Use split: "path"
to group by the first path segment (e.g. /v2/usercollection/... → usercollection.ts). Types referenced by more than one group are placed in
common.ts.
Default header (when includeHeader is true and headerComment is not set):
- Tool name:
@pinta365/openapi-typegen - Generated-at timestamp (ISO)
- “Source file:” line when
sourceLabelis set - “DO NOT EDIT THIS FILE MANUALLY”
- OpenAPI 2: Uses
definitions; external$refURLs are fetched and merged (including docs that use adefinitionsorcomponents.schemaswrapper). - OpenAPI 3.x: Uses
components.schemas; external$refURLs are fetched and merged the same way. - External refs: Both specs support
$refto full URLs (e.g.https://example.com/schemas.json#/Thing). The library fetches those documents and resolves refs recursively; use a customresolverwhen you need to serve from disk or a different source. - Type names are PascalCase; property names follow
propertyNaming. Descriptions and titles become JSDoc (single-line or starred-block for multi-line).
