About openapi to typescript
Generate TypeScript interfaces and types from the schemas in an OpenAPI file, so your front end matches the API.
Benefits
- Compile-time errors when the API changes.
- Autocomplete for response fields.
- Less hand-written boilerplate.
Review
Generated types describe what the schema allows. They do not guarantee the server really returns that shape, so validate at run time for critical data.
A worked example
A schema User with an integer id, a string email and an optional nickname becomes an interface with id: number, email: string and nickname?: string. Enums become union types such as 'active' | 'blocked', and references to other schemas become references to other interfaces.
Keep types in sync
- Regenerate the types whenever the specification changes.
- Do it in CI so that mismatches are caught.
- Validate responses at run time for important data, since types are removed after compiling.
