5 ms·
I wish OpenAPI had a stronger mapping to typescript types.
by klysm 3y ago
I wish OpenAPI had a stronger mapping to typescript types.
- djbusby 3y agoOr let us describe an custom type (GUID/ULID/UUID)
- adobrawy 3y agoDo you mean TypeAPI?
- theogravity 3y agoThis is a problem I also encountered. I created my own OpenAPI generator with hacks around the AST generation to support a field for specifying the TS type of the Request / Response If the spec supports a field to specify the type for a component, that would help a lot with static type generation.
- eyelidlessness 3y agoIt’s definitely possible, with a (fair) bit of glue code. In the past, I wrapped io-ts with OpenAPI docgen logic. This worked great: ~every network boundary type was defined once, providing runtime parse/validation and serialization, inferred static types, and API documentation all at once. Unfortunately I didn’t get permission to open source the solution. But if I ever get bandwidth to build its spiritual successor, I’ll tackle it from the opposite direction: the OpenAPI/JSON Schema specs are very well suited to define the underlying primitives for a zod/io-ts like solution. The great thing about inverting the direction is that it defers “generating” anything to the static type system, ie there’s much less surface area for the runtime and doc schemas to diverge. It also provides opportunities to enrich static types with more information in a general way (eg “brand” a number with its valid range, or a string as a valid email address). This would allow the benefits of runtime checks at API boundaries to be treated as static checks internally without additional/redundant internal runtime overhead.
- klysm 3y agoI’ve been wanting to write a tool that takes in a open api spec and generates zod/io-ts for a while now but I haven’t had the time. That sounds very useful though unfortunate you can’t open it up!
- kelnos 3y agoIsn't that more a function of the code generator you use, than the specification format itself?
- varanauskas 3y agoI’m trying to achieve generator-less mapping from OpenAPI to Typescript types at https://github.com/varanauskas/oatx https://github.com/varanauskas/oatx
- klysm 3y agoNice! So this goes OpenAPI -> JSON Schema -> typescript?
- varanauskas 3y agoOpenAPI uses JSONSchema to define object (e.g. request, response body) schemas, so it would be more accurate to say it: Extracts the JSONSchema based on path+method+response type from OpenAPI and then converts that to typescript
- revskill 3y agoIt's possible to use Typescript Class to map to openapi spec.