OpenAPI to TypeScript
Generate TypeScript interfaces and type aliases from OpenAPI 3.x components.schemas or Swagger 2.0 definitions, handling $ref, allOf inheritance, nullable, enums, arrays and additionalProperties — from YAML or JSON input.
Runs in your browserEvery computation happens in your browser — your data never leaves this device.
Generated TypeScript
Paste an OpenAPI / Swagger document to generate types
What this tool does
- Your backend only ships an OpenAPI / Swagger document: generate the TypeScript types instead of hand-writing interfaces from the docs.
- Produce type definitions for an SDK, tests or mock data so typos in field names and missing required fields fail at compile time.
- Compare model definitions across API versions by generating types for each and diffing the result in your project.
- Migrate a legacy internal Swagger 2.0 document to TypeScript and replace `any` with real types step by step.
Example
Input
openapi: 3.0.3
components:
schemas:
Pet:
type: object
description: A pet
required: [id, name]
properties:
id: { type: integer }
name: { type: string }
tag: { type: string, nullable: true }
status: { type: string, enum: [available, pending, sold] }
friends:
type: array
items: { $ref: '#/components/schemas/Pet' }
Dog:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
required: [bark]
properties:
bark: { type: boolean }Output
/** A pet */
export interface Pet {
id: number;
name: string;
tag?: string | null;
status?: "available" | "pending" | "sold";
friends?: Pet[];
}
export interface Dog extends Pet {
bark: boolean;
}nullable becomes `| null`, enums become literal unions, and anything outside required gets a `?`. Inside allOf, plain $ref members become `extends` while inline object members are merged into the interface body.
Frequently asked questions
Is OpenAPI 3.1 supported?
Yes. Schemas in 3.1 are plain JSON Schema and are read with the same rules from components.schemas. The only difference is that 3.1 expresses nullability as `type: ["string", "null"]`, which also becomes `string | null` here.
How is allOf handled?
If every allOf member is a plain `$ref`, the output is `interface X extends A, B`. Inline object members (those with properties) have their properties and required list merged into the same interface body. If a member is a scalar or a union that cannot be extended, it falls back to an intersection type such as `type X = A & B`.
Can $ref resolve across files?
No. The type name is derived from the last segment of the reference (`#/components/schemas/Pet` → `Pet`) without reading other files or the network. As long as the referenced schema also lives in the same document's components.schemas or definitions, the generated code compiles as-is.
Can I use the generated code directly?
Yes. The output is plain interfaces and type aliases with no runtime dependency. The few things that cannot be resolved statically (duplicate schema names, a $ref without a usable name) are reported as notes and downgraded to unknown instead of silently emitting wrong code.
Is my document uploaded anywhere?
No. Parsing and generation run entirely in your browser with JavaScript, the page makes no network requests, and it keeps working with the network disconnected.
Keywords:openapiopenapi 转 typescriptswaggerswagger 转 tstypescript 类型codegenapi 类型生成json schema接口定义openapi to types