OpenAPI to TypeScript converter
Convert OpenAPI to TypeScript: paste an OpenAPI 3.0 or 3.1 spec, or Swagger 2.0, as YAML or JSON, and get a TypeScript type for every component schema, request and response types for each operation, and validate.ts: isX/parseX functions that validate API data against every rule of the spec, with no dependencies.
What you get
- TypeScript types from OpenAPI for every component schema, plus request and response types for each operation, from OpenAPI 3.0/3.1 or Swagger 2.0 in YAML or JSON.
- validate.ts to validate API responses in TypeScript: parsePet(await res.json()) returns a typed Pet or lists every problem.
- Checked: compiled in strict mode, and the validators agree with Ajv on every test value.
- Private: internal specs stay on your computer, since everything runs in your browser.
How each result is checked
- Every schema is read from the document and compiled with Ajv, the most widely used JSON Schema validator.
- types.ts and validate.ts are compiled together with the real TypeScript compiler in strict mode.
- validate.ts is run for every schema: isX must agree with Ajv on every test value, and the values it accepts must type-check against their types.
- Examples from the spec are type-checked against their types.
What can't convert exactly
- Operations get types named after their operationId, for example createPet gives CreatePetRequest and CreatePetResponse. Parameters (path, query, headers) are not converted yet.
- In OpenAPI 3.0, keywords next to a $ref are ignored, as the specification says; in 3.1 they apply. You get a note when this matters.
- TypeScript types cannot enforce rules like minLength or maximum; validate.ts checks them at run time. Very large specs (thousands of schemas) take longer with validate.ts on.
- References to other files cannot be followed here and become "any value", with a warning.
Questions
How do I validate an API response against the generated types?
const pet = parsePet(await res.json()) gives a typed Pet or throws a ValidationError listing each problem with its JSON pointer; isPet(data) narrows without throwing.
Which versions are supported?
OpenAPI 3.0 and 3.1, and Swagger 2.0, in YAML or JSON.
What about nullable?
OpenAPI 3.0 "nullable: true" and 3.1 type lists such as ["string", "null"] both become "| null", and validate.ts accepts null there.
How do I convert Swagger to TypeScript?
Paste your Swagger 2.0 or OpenAPI 3.x document (YAML or JSON). This OpenAPI TypeScript generator writes a type for every schema and operation, plus validators, without installing anything.
Does it accept Swagger 2.0 and OpenAPI 3.1, in JSON or YAML?
Yes: Swagger 2.0 to TypeScript works the same as OpenAPI 3.0 and OpenAPI 3.1, and the document can be JSON or YAML. Paste it or open the file.
Is my OpenAPI document sent to a server?
No. The spec is read and converted in your browser, so private and internal APIs stay on your computer.
How do I convert OpenAPI to TypeScript online?
Paste your OpenAPI or open a file on the left (or click Example). The TypeScript appears on the right as you type, already checked. Copy it, or download it as a file. Nothing is uploaded.
Can the TypeScript output be wrong?
Every result is checked before it is shown (the checks are listed on this page). If a check fails, the page says "Review needed" and why, instead of presenting the output as correct, and anything that cannot convert exactly is named in the notes.