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
5
The code is [MIT-licensed](./LICENSE) and free for use.
6
6
7
+
> **Tip:**
8
+
> 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.
9
+
7
10
## Features
8
11
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
12
+
- β Supports OpenAPI 3.0 and 3.1 (including advanced features like [discriminators](https://spec.openapis.org/oas/v3.1.0#discriminator-object))
13
+
- β Generate **runtime-free types** that outperform oldschool codegen
11
14
- β Load schemas from YAML or JSON, locally or remotely
12
-
- β Native Node.js code is fast and generates types within milliseconds
15
+
- β Generate types for even huge schemas within milliseconds
16
+
17
+
_Note: OpenAPI 2.x is supported with versions `5.x` and previous_
13
18
14
19
## Examples
15
20
16
21
π [See examples](./examples/)
17
22
18
23
## Setup
19
24
20
-
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:
25
+
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
26
22
27
```bash
23
28
npm i -D openapi-typescript typescript
24
29
```
25
30
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))
31
+
And in your `tsconfig.json`, to load the types properly:
32
+
33
+
```diff
34
+
{
35
+
"compilerOptions": {
36
+
+ "module": "ESNext", // or "NodeNext"
37
+
+ "moduleResolution": "Bundler" // or "NodeNext"
38
+
}
39
+
}
40
+
```
41
+
42
+
> **Highly recommended**
43
+
>
44
+
> Also adding the following can boost type safety:
45
+
> ```diff
46
+
> {
47
+
> "compilerOptions": {
48
+
> + "noUncheckedIndexedAccess": true
49
+
> }
50
+
> }
51
+
> ```
29
52
30
53
## Basic usage
31
54
32
-
First, generate a local type file by running `npx openapi-typescript`:
55
+
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.
47
-
48
-
Then, import schemas from the generated file like so:
67
+
Then in your TypeScript project, import types where needed:
49
68
50
69
```ts
51
70
importtype { paths, components } from"./my-openapi-3-schema"; // generated by openapi-typescript
0 commit comments