Generating TypeScript from OpenAPI schemas
OpenAPI schemas in, TypeScript types out.Generate TypeScript interfaces and type aliases from components.schemas. Types only: no HTTP
client or runtime validation.
Generate a module
Import github.com/pb33f/libopenapi/generator/typescript.
With a parsed v3 model:
file, err := typescript.RenderSchemas(model.Model.Components.Schemas)
if err != nil {
return err
}
for _, d := range file.Diagnostics {
fmt.Printf("[%s] %s: %s\n", d.Code, d.Path, d.Message)
}
if err := os.WriteFile("api.gen.ts", file.Source, 0o644); err != nil {
return err
}
file.Source contains the module; file.Types lists its exported names.
For a Product schema with required id and name, an optional status enum, and a nullable description:
export interface Product {
id: string;
name: string;
status?: "draft" | "active";
description?: string | null;
}
Use import type { Product } from "./api.gen" in your code.
Schema mapping
| Schema | TypeScript |
|---|---|
| Object with properties | Interface; nested objects stay inline. |
| Optional / nullable | name?: T / T | null. |
| Scalar enum / const | Literal union / literal. |
allOf |
Intersection. |
oneOf / anyOf |
Union. |
| Array | T[]. |
| String format / integer | string / number. |
Descriptions become JSDoc. Valid component names stay unchanged; renames and collisions appear in
Diagnostics. Use WithTypeNameResolver for custom names and WithHeaderComment for a custom header.
Limits
Bundle external references into local components, then reparse
before generation. Request and response types are shared; readOnly and writeOnly are not enforced.
Some composition shapes lose detail. Check diagnostics and run your TypeScript compiler against the
output and its consumers. The TypeScript package’s README.md lists the full mapping and limits.
For Go output, see Generating Code or Generating SDKs.