OpenAPI → TypeScript ジェネレーター
OpenAPI 3.0/3.1 仕様(YAML/JSON)からブラウザで TypeScript インターフェースを生成。$ref・oneOf/allOf・enum・nullable 対応、Zod スキーマも同時出力可。
- ブラウザ内で処理
- ツールの入力を ZeroTool のサーバーにアップロードしません
- 無料 · 登録不要
WeChat でスキャンしてシェア
例・詳しい説明・よくある質問 実際の出力つきの例、ほかのツールとの違い、よくある質問。
生成される内容
- components/schemas:各スキーマが
export interfaceまたはexport typeになります。オブジェクトは interface、プリミティブ / union / enum / intersection は type alias として出力されます。 - $ref:
#/components/schemas/...への参照は型名で解決され、仕様の宣言順を維持します。 - oneOf / anyOf:union 型
A | B | C。allOf は intersectionA & Bになります。 - enum:文字列または数値のリテラル union。
- nullable:3.0 の
nullable: trueも 3.1 のtype: ['X', 'null']もX | nullに変換。 - format ヒント:
format: date-timeには ISO 8601 の JSDoc を付与し、format: binaryはBlobになります。 - additionalProperties:インデックスシグネチャまたは
Record<string, T>。
Path 型(任意)
path 型を含める を有効にすると、各オペレーションに型エンベロープが生成されます:
export namespace Components {
export namespace GetPetById {
export interface PathParameters {
id: number;
}
export type Response200 = Pet;
}
}
operationId がない場合は method と path から合成します(例:GetPetsId)。
Zod スキーマ(任意)
Zod スキーマを含める を有効にすると、各コンポーネントに対して XSchema を並行して出力します:
import { z } from 'zod';
export const PetSchema = z.object({
id: z.number().int(),
name: z.string(),
status: z.enum(["available", "pending", "sold"]),
});
OpenAPI 仕様ではなく既存の手書き TS 型から Zod を作りたい場合は TypeScript → Zod を使ってください。
例:組み込みの Pet Store 仕様
サンプルを押すと、Pet と NewPet の 2 スキーマと 3 つのオペレーションを持つ小さな OpenAPI 3.0.3 仕様が読み込まれます。出力のスキーマ部分です。
export interface Pet {
id: number;
name: string;
tag?: string | null;
status: "available" | "pending" | "sold";
photo?: Blob;
}
export interface NewPet {
name: string;
tag?: string;
}
必須プロパティは必須のまま、それ以外には ? が付きます。nullable: true は | null になり、format: binary は Blob になります。path 型を含めるをオンにすると、getPetById は PathParameters { id: number } と Response200 = Pet を持つ Components.GetPetById になります。Zod スキーマを含めるをオンにして生成した PetSchema は(zod 3.25 で確認)、status: "lost" を拒否し、tag: null を受け付けます。
例:const・型の配列・外部参照
次の OpenAPI 3.1 スキーマは const、型の配列、別ファイルへの参照を使っています。
openapi: 3.1.0
info: { title: Events, version: 1.0.0 }
paths: {}
components:
schemas:
Event:
type: object
required: [id, kind, createdAt]
properties:
id: { type: integer, format: int64 }
kind: { const: order.created }
createdAt: { type: string, format: date-time }
note: { type: [string, 'null'] }
owner: { $ref: 'https://example.com/schemas/user.yaml#/User' }
ツールの出力です。
export interface Event {
id: number;
kind: "order.created";
/** ISO 8601 date-time */ createdAt: string;
note?: string | null;
owner?: unknown /* unresolved $ref: https://example.com/schemas/user.yaml#/User */;
}
type: [string, "null"] は string | null に、const はリテラル型 "order.created"(Zod では z.literal("order.created"))になります。user.yaml への参照は別のドキュメントにあり、ツールは取得しません。owner は unknown になり、参照はコメントに残ります。ステータス欄には「非対応:https://example.com/schemas/user.yaml#/User への $ref」と表示されます。先に redocly bundle で外部ファイルを components にまとめてから、まとめた仕様を貼り付けてください。
例:Zod の前方参照と再帰
Zod スキーマは JavaScript の定数なので、定義する行より前では使えません。次のスキーマでは、Order が後で宣言される Customer を参照し、Node は自分自身を参照します。
components:
schemas:
Order:
type: object
required: [customer]
properties:
customer: { $ref: '#/components/schemas/Customer' }
Customer:
type: object
required: [name]
properties:
name: { type: string }
Node:
type: object
required: [value]
properties:
value: { type: string }
children:
type: array
items: { $ref: '#/components/schemas/Node' }
Zod の出力では、どちらの参照も z.lazy で包みます。スキーマを探すのはモジュールの読み込み時ではなく、データを検証するときになります。直接または他のスキーマを経由して自分自身を参照するスキーマには z.ZodType<Node> の型注釈も付けます。自分の初期化式に現れる定数の型を TypeScript は推論できないためです(エラー TS7022)。
export const OrderSchema = z.object({
customer: z.lazy(() => CustomerSchema),
});
export const CustomerSchema = z.object({
name: z.string(),
});
export const NodeSchema: z.ZodType<Node> = z.object({
value: z.string(),
children: z.array(z.lazy(() => NodeSchema)).optional(),
});
生成したモジュールは TypeScript 5.9 の strict モードでコンパイルでき、zod 3.25 で 3 階層のツリーを検証できます。
制限
- 解決するのは
#/components/schemas/...の参照と、path 型での#/components/parameters/...だけです。それ以外の参照(URL、別ファイル、#/components/responses/...)はunknownになり、ステータス欄に一覧が出ます。 - TypeScript の識別子として使えないスキーマ名は変更されます。英数字、
_、$以外の文字は_に(pet-status→pet_status)、数字で始まる名前は先頭に_を、unknownやclassなどのキーワードは末尾に_を付けます。 - 値がオブジェクトや配列の
constはunknownになります。文字列・数値・真偽値・null はリテラル型になります。 format: int64のintegerはnumberになり、253 を超えると精度が落ちます。API がそうした ID を文字列で送るならstringに変えてください。- path 型が読み取るのは
application/jsonのリクエストボディとレスポンスボディだけです。
関連ツール
- OpenAPI・Swagger 検証ツール — 型生成前に仕様を検証。
- JSON → TypeScript — 仕様はなく JSON レスポンス例だけがある場合に。
- JSON → Zod スキーマ — 単一の JSON サンプルから Zod を生成。
FAQ
対応している OpenAPI のバージョンは?
3.0.x と 3.1.x の両方に対応します。OpenAPI 3.1 の `type: ['string', 'null']` は `string | null` として、OpenAPI 3.0 の `nullable: true` も同様に解釈されます。
Swagger 2.0 は使えますか?
設計上、Swagger 2.0 は非対応です。先に editor.swagger.io(Swagger Editor の内蔵コンバーター)で OpenAPI 3.x に移行してから貼り付けてください。
リモート $ref は解決されますか?
されません。解決するのは `#/components/schemas/...`(path 型では `#/components/parameters/...` も)だけです。URL や別ファイルなど、それ以外の `$ref` はコメント付きの `unknown` になり、未解決の参照はすべてステータス欄に表示されます。先に `redocly bundle` などで仕様を 1 ファイルにまとめるか、openapi-typescript CLI を使ってください。
仕様はサーバーに送られますか?
送られません。YAML や JSON はこのタブの中で解析して TypeScript を生成し、仕様のアップロードもブラウザーのストレージへの保存もしません。リモートの $ref の URL も取得しません。仕様や名前空間の編集を確定したとき、オプションを変えたとき、またはサンプルを読み込み、未解決の参照なしで型を生成できたときに、ツール名と操作 generate だけを記録します。入力中は generate を記録せず、同じ出力は重複して数えません。「サンプル」を押すと別に example_load を記録します。仕様や生成した型の内容は含みません。
Zod スキーマも一緒に得られますか?
はい。**Zod スキーマを含める** をオンにすると、TypeScript インターフェースに加えて `z.object({...})` も出力します。静的型と一致するランタイム検証が必要な場合に便利です。
「path 型を含める」は何をしますか?
各オペレーションごとに名前空間を生成し、`PathParameters`・`QueryParameters`・`RequestBody`・`Response200` / `Response201` などを公開します。DTO インターフェースだけ欲しい場合はオフのままで構いません。