OpenAPI to TypeScript
Converts OpenAPI 3.0 specifications to TypeScript interfaces and type guards.
Input: OpenAPI file (JSON or YAML) Output: TypeScript file with interfaces and type guards
When to Use
- "generate types from openapi"
- "convert openapi to typescript"
- "create API interfaces"
- "generate types from spec"
Workflow
- Request the OpenAPI file path (if not provided)
- Read and validate the file (must be OpenAPI 3.0.x)
- Extract schemas from
components/schemas - Extract endpoints from
paths(request/response types) - Generate TypeScript (interfaces + type guards)
- Ask where to save (default:
types/api.tsin current directory) - Write the file
OpenAPI Validation
Check before processing:
If invalid, report the error and stop.
Type Mapping
Primitives
Format Modifiers
Complex Types
Object:
Array:
Enum:
oneOf (Union):
allOf (Intersection/Extends):
Code Generation
File Header
Interfaces (from components/schemas)
For each schema in components/schemas:
- Use OpenAPI description as JSDoc
- Fields in
required[]have no? - Fields outside
required[]have?
Request/Response Types (from paths)
For each endpoint in paths:
Naming convention:
{Method}{Path}Requestfor params/body{Method}{Path}Responsefor response
Type Guards
For each main interface, generate a type guard:
Type guard rules:
- Check
typeof value === 'object' && value !== null - For each required field: check
'field' in value - For primitive fields: check
typeof - For arrays: check
Array.isArray() - For enums: check
.includes()
Error Type (always include)
$ref Resolution
When encountering {"$ref": "#/components/schemas/Product"}:
- Extract the schema name (
Product) - Use the type directly (don't resolve inline)
Complete Example
Input (OpenAPI):
Output (TypeScript):


