DevBolt
Processed in your browser. Your data never leaves your device.

How do I convert an OpenAPI spec to TypeScript?

Paste your OpenAPI 3.x or Swagger 2.0 spec in JSON or YAML format and instantly get TypeScript interfaces and types. The tool resolves $ref references, handles allOf/oneOf/anyOf, generates enums, and creates API operation types. Download the .ts file or copy to clipboard. Everything runs in your browser.

Convert schema to TypeScript
Input
components:
  schemas:
    User:
      type: object
      required: [id, name, email]
      properties:
        id: { type: integer }
        name: { type: string }
        email: { type: string }
        role:
          type: string
          enum: [admin, user, guest]
Output
export interface User {
  id: number;
  name: string;
  email: string;
  role?: "admin" | "user" | "guest";
}
← Back to tools

OpenAPI to TypeScript Converter

Paste an OpenAPI 3.x or Swagger 2.0 spec (JSON or YAML) and generate TypeScript interfaces, types, and API operation types. Handles $ref resolution, allOf/oneOf/anyOf, enums, and nested objects.

Options

Samples:

How It Works

Schema Conversion

Extracts all schemas from components.schemas (OpenAPI 3.x) or definitions (Swagger 2.0) and generates TypeScript interfaces or type aliases with proper types, optional fields, and nested objects.

$ref Resolution

Follows $ref pointers within the spec to resolve references to other schemas, producing clean type names (e.g., $ref: "#/components/schemas/Pet" Pet).

Composition Types

Handles allOf (intersection types), oneOf and anyOf (union types), and enum values as string literal unions.

API Operation Types

Optionally generates typed path parameters, query parameters, request bodies, and response types from your API's path definitions. Uses operationId for naming.

Supported Features

Schema Types: string, number, integer, boolean, object, array
Composition: allOf, oneOf, anyOf, $ref
Enums: String and numeric enum → union types
Nullable: nullable: true → Type | null
Maps: additionalProperties → Record<string, T>
Output: interface or type alias, export, readonly, JSDoc
Specs: OpenAPI 3.0.x, 3.1.x, Swagger 2.0
Operations: Path, query, body, and response types

Tips & Best Practices

Pro Tip

Generate both request and response types for full coverage

An OpenAPI spec defines request bodies, query parameters, path parameters, and response schemas. Generate types for all of them — not just responses. This gives you type-safe API clients where both the data you send and receive are validated at compile time.

Common Pitfall

oneOf/anyOf/discriminatedUnion schemas need manual refinement

OpenAPI's oneOf and anyOf map to TypeScript union types, but the generated unions are often too broad. If your API uses discriminated unions (type: 'email' | type: 'sms'), add a discriminator to the OpenAPI spec so the generated types include proper type narrowing.

Real-World Example

Generate a type-safe API client from your Swagger spec

Paste your openapi.json, generate TypeScript interfaces, then use them with a typed fetch wrapper. Every endpoint gets a function with typed parameters and return type. When the API changes, regenerate types and TypeScript immediately flags all the places your code needs to update.

Security Note

Validate responses even with generated types

Generated types provide compile-time safety but not runtime safety. A compromised or buggy API might return data that doesn't match the spec. Use Zod schemas (convert types to Zod with DevBolt's JSON to Zod tool) for runtime validation of critical API responses.

Frequently Asked Questions

How do I generate TypeScript types from an OpenAPI specification?
Paste your OpenAPI 3.x or Swagger 2.0 specification in JSON or YAML format into DevBolt's converter. It parses all schema definitions, path parameters, request bodies, and response types, generating corresponding TypeScript interfaces. Each schema component becomes a named interface. Nested objects create referenced types. Enum values become TypeScript union types or enums. The output is ready to use in your frontend or API client code for type-safe HTTP requests. Everything runs client-side.
What parts of an OpenAPI spec are converted to TypeScript?
The converter generates types from: component schemas (the main data models), path operation parameters (query, path, header), request body schemas, response body schemas for each status code, enum definitions, and allOf/oneOf/anyOf compositions. allOf produces intersection types, oneOf/anyOf produce union types. Nullable fields become optional or null union types. Required fields are non-optional in the generated interfaces. The $ref references are resolved to produce clean, self-contained type definitions.
How are OpenAPI request and response schemas mapped to TypeScript?
Request body schemas become input interfaces with the request content type's schema properties. Response schemas are generated per status code, so a 200 response and a 400 error response produce different types. Path parameters become function argument types. Query parameters produce an options interface with optional fields for non-required params. Common patterns like pagination, error responses, and list/detail endpoints generate reusable generic types. The converter preserves descriptions as JSDoc comments when enabled.

Related Convert Tools