Problem: Manually creating TypeScript types is time-consuming, tedious, and...


Problem: Manually creating TypeScript types is time-consuming, tedious, and error prone.

Solution: Generate the types if you can.

Here are 4 examples:

1. Prisma

Prisma is an ORM tool that generates TypeScript types based on your database schema. It provides a type-safe way to interact with your DB and ensures that your queries are type-checked at compile time. You get excellent autocomplete support as you write your queries too. Prisma generates types for your models, queries, and mutations.

2. Supabase

Supabase provides a CLI for generating TypeScript types for your database schema. This means you can use the generated types in your client code to ensure type safety when interacting with your DB.

3. Open API with Swagger Codegen or OpenAPI Generator

Open API is a spec for defining RESTful APIs. It allows you to describe your API endpoints, request and response types, and auth methods in a standardized format. Tools like Swagger Codegen or OpenAPI Generator can generate TypeScript types from your Open API spec. This ensures your types are in sync with your API and provides a type-safe way to interact with your API.

4. GraphQL Codegen or Apollo

If you're using GraphQL, then GraphQL Codegen and Apollo are two projects that generate TypeScript types from your GraphQL schema and queries. So you don't have to write types manually, and you know your types are always in sync with your GraphQL API. These tools can generate types for queries, mutations, and subscriptions.

This isn't an exhaustive list, but the point is, I try to avoid manually creating types when possible.

Using tools that generate types based on the DB or API definition saves time, and ensures the types remain accurate.

View original on X