TypeScript Integration
Our API comes fully equipped with an automatically generated OpenAPI specification. This means that with the right tools, you can ensure that your requests are being made with the correct parameters, and that your responses have what you expect - and all without hardcoding any types.
Getting Started
For this guide, we'll be using openapi-typescript
, an open-source library that consumes OpenAPI specifications and outputs TypeScript type definitions. We'll need to install that first.
Run the following command in your project:
Next, you'll want to generate the type definitions, like so:
I've chosen to output the types into src/types/generated
for convenience, but you can put the output anywhere that makes sense. Regardless of where you put it, you should add it to your project's .gitignore
if you're using version control.
Now that you have the type definitions, you can create type aliases for the types you'll be using. Let's say you want to manipulate course data, so you'll need the types of the request object and of the response objects. This can be accomplished like so:
As you can see by hovering over the CourseParams
and CourseResponse
type aliases, they have the shape that you would expect, and you can use them to ensure the type-safety when interacting with the API.