You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
openapi-typescript generates TypeScript types from static <ahref="https://spec.openapis.org/oas/latest.html"target="_blank"rel="noopener noreferrer">OpenAPI</a> schemas quickly using only Node.js. It is fast, lightweight, (almost) dependency-free, and no Java/node-gyp/running OpenAPI servers necessary.
3
+
openapi-typescript turns [OpenAPI 3.0 & 3.1](https://spec.openapis.org/oas/latest.html) schemas into TypeScript quickly using Node.js. No Java/node-gyp/running OpenAPI servers necessary.
4
4
5
-
The code is [MIT-licensed](./LICENSE) and free for use.
5
+
The code is [MIT-licensed](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-typescript/LICENSE") and free for use.
6
+
7
+
::: tip
8
+
9
+
New to OpenAPI? Speakeasy’s [Intro to OpenAPI](https://www.speakeasyapi.dev/openapi) is an accessible guide to newcomers that explains the “why” and “how” of OpenAPI.
10
+
11
+
:::
6
12
7
13
## Features
8
14
9
-
- ✅ Supports OpenAPI 3.0 and 3.1 (including advanced features like <ahref="https://spec.openapis.org/oas/v3.1.0#discriminator-object"target="_blank"rel="noopener noreferrer">discriminators</a>)
10
-
- ✅ Generate **runtime-free types** that outperform old-school codegen
15
+
- ✅ Supports OpenAPI 3.0 and 3.1 (including advanced features like [discriminators](https://spec.openapis.org/oas/v3.1.0#discriminator-object))
16
+
- ✅ Generate **runtime-free types** that outperform oldschool codegen
11
17
- ✅ Load schemas from YAML or JSON, locally or remotely
12
-
- ✅ Native Node.js code is fast and generates types within milliseconds
18
+
- ✅ Generate types for even huge schemas within milliseconds
19
+
20
+
_Note: OpenAPI 2.x is supported with versions `5.x` and previous_
This library requires the latest version of <ahref="https://nodejs.org/en"target="_blank"rel="noopener noreferrer">Node.js</a> installed (20.x or higher recommended). With that present, run the following in your project:
28
+
This library requires the latest version of [Node.js](https://nodejs.org) installed (20.x or higher recommended). With that present, run the following in your project:
21
29
22
30
```bash
23
31
npm i -D openapi-typescript typescript
24
32
```
25
33
26
-
> ✨ **Tip**
27
-
>
28
-
> Enabling [noUncheckedIndexedAccess](https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess) in `tsconfig.json` can go along way to improve type safety ([read more](../../docs/src/content/docs/advanced.md#enable-nouncheckedindexedaccess-in-your-tsconfigjson))
34
+
And in your `tsconfig.json`, to load the types properly:
35
+
36
+
::: code-group
37
+
38
+
```json [tsconfig.json]
39
+
{
40
+
"compilerOptions": {
41
+
"module": "ESNext", // or "NodeNext" // [!code ++]
42
+
"moduleResolution": "Bundler"// or "NodeNext" // [!code ++]
43
+
}
44
+
}
45
+
```
46
+
47
+
:::
48
+
49
+
::: tip Highly recommended
50
+
51
+
Also adding the following can boost type safety:
52
+
53
+
::: code-group
54
+
55
+
```json [tsconfig.json]
56
+
{
57
+
"compilerOptions": {
58
+
"noUncheckedIndexedAccess": true// [!code ++]
59
+
}
60
+
}
61
+
```
62
+
63
+
:::
29
64
30
65
## Basic usage
31
66
32
-
First, generate a local type file by running `npx openapi-typescript`:
67
+
First, generate a local type file by running `npx openapi-typescript`, first specifying your input schema (JSON or YAML), and where you’d like the `--output` (`-o`) to be saved:
> ⚠️ Be sure to <ahref="https://redocly.com/docs/cli/commands/lint/"target="_blank"rel="noopener noreferrer">validate your schemas</a>! openapi-typescript will err on invalid schemas.
79
+
Then in your TypeScript project, import types where needed:
47
80
48
-
Then, import schemas from the generated file like so:
81
+
::: code-group
49
82
50
-
```ts
83
+
```ts [src/my-project.ts]
51
84
importtype { paths, components } from"./my-openapi-3-schema"; // generated by openapi-typescript
0 commit comments