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

OpenAPI to TypeScript Interfaces

A practical guide to generating TypeScript interfaces from OpenAPI specifications, covering schema types, composition patterns, and API operation typing.

← 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

Interface vs type alias

TypeScript interfaces support declaration merging and are generally preferred for object shapes. Type aliases are required for union types (oneOf/anyOf) and intersection types (allOf). DevBolt lets you choose your preferred style. For schemas that use composition, type aliases are used automatically regardless of your setting.

Handling enums

OpenAPI enums are converted to TypeScript string literal union types (e.g., type Status = "active" | "inactive" | "pending"). This is more idiomatic in TypeScript than using the enum keyword, as it works better with type narrowing and doesn't generate any runtime JavaScript.

Nullable and optional fields

Fields listed in the 'required' array become required TypeScript properties; others get the optional marker (?). The nullable: true flag adds '| null' to the type. You can also toggle 'all optional' to make every field optional, useful when generating types for PATCH endpoints or partial updates.

Frequently Asked Questions

Should I use interfaces or type aliases for OpenAPI types?

Use interfaces for plain object schemas (they support declaration merging and are more readable). Use type aliases for schemas using allOf/oneOf/anyOf, as these require intersection or union types which can't be expressed with interfaces.

How are additionalProperties converted to TypeScript?

OpenAPI additionalProperties maps to TypeScript's Record type. For example, additionalProperties: { type: string } becomes Record<string, string>. Untyped additionalProperties become Record<string, unknown>.

Related Convert Tools