openapi-typescript is a powerful command-line tool and library designed to convert OpenAPI 3.0 and 3.1 specifications into TypeScript type definitions. It produces runtime-free, statically-analyzable types, enabling robust type safety for API clients, facilitating validation of mock data, and streamlining the development of business logic directly from your API's schema. The current stable version is 7.13.0, and the project maintains a healthy and active release cadence, frequently publishing minor or patch versions across its ecosystem of packages (e.g., openapi-typescript, openapi-fetch). A key differentiator is its speed and its minimal dependency footprint, requiring only Node.js and avoiding external runtimes like Java, making it a lightweight and efficient alternative to many traditional OpenAPI codegen solutions. It supports loading schemas from local YAML or JSON files, as well as remote URLs.
npm install openapi-typescriptVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to generate TypeScript types from an OpenAPI schema using the `openapi-typescript` CLI and then how to import and use the generated `paths` and `components` types in a TypeScript application to ensure type safety for API interactions. It fetches a remote Swagger Petstore schema for demonstration.
Upgrade your OpenAPI schema to 3.0 or 3.1, or downgrade `openapi-typescript` to a 5.x version if OpenAPI 2.x support is essential.
Consult the migration guide for 7.x on the official documentation site. Update your build scripts, programmatic API calls, and potentially introduce a `redocly.config.yml` file.
Add `"noUncheckedIndexedAccess": true` to your `compilerOptions` in `tsconfig.json`.
Update your `compilerOptions` in `tsconfig.json` to include `"module": "ESNext"` and `"moduleResolution": "Bundler"`.
If using this flag, ensure your consuming code and any associated client libraries (like `openapi-fetch`) are compatible with these type markers. Adjust your code to use the `Readable<T>` or `Writable<T>` helpers if you need to filter these properties.
Ensure the output file path in your `npx openapi-typescript` command matches the import path in your TypeScript files. Verify your `tsconfig.json` includes the directory of the generated file and has correct `moduleResolution` settings (e.g., `"moduleResolution": "Bundler"`). Run the generation command before TypeScript compilation.
Enable `"noUncheckedIndexedAccess": true` in your `tsconfig.json`. Alternatively, use optional chaining (`?.`), nullish coalescing (`??`), or explicit `if` checks to safely access properties.
Review the programmatic usage documentation for `openapi-typescript` (e.g., `import openapiTS from 'openapi-typescript'; await openapiTS(input);`). If using `@hey-api/openapi-ts`, ensure you're using `createClient` correctly with its specific configuration.
Update your OpenAPI specification to version 3.0 or 3.1. If this is not possible, downgrade `openapi-typescript` to a 5.x version, which supports OpenAPI 2.x schemas.