Inference
We can infer the types of any OpenAPI spec using the inference helpers provided by @webroute/oas
, without any code generation.
This way our OpenAPI spec is directly transformed into TypeScript form, without any intermediary build or generation steps required.
Setup
To ensure correct type inference, we should place our OpenAPI spec into a .d.ts
file:
This helps TypeScript infer the correct types for literal types, like { type: "object" }
instead of { type: string }
. It also makes our OpenAPI spec "real" source code now, which will not be bundled into JavaScript outputs.
The TypeScript team are actively discussing ways to support strongly-typed JSON import types here.
Client
We can use this alongside @webroute/client
as a "headless", type-safe client
Learn more in the client docs.