Supabase will soon release native type generators that dump your database types for various languages. For now, we support TypeScript through a third-party tool.
supabase-js ships with type definitions for usage with TypeScript and for convenient IntelliSense auto-complete and documentation in your editor.
When using TypeScript, you can pass the type of database row as a type parameter to the
from method to get better auto-completion support down the chain.
If you don't provide a type for the row you need to explicitly pass
Supabase generates an OpenAPI specification file for your database which can be used to generate your data types for usage with TypeScript.
The OpenAPI specification for your Supabase project can be accessed as follows:
Using the open source openapi-typescript tool you can generate your types and store them locally:
- Since the generator uses JSON API, there is no way to determine if a column is an Array. It will generate array types as
string, even though Supabase handles this automatically and returns arrays. You can fix this manually in the files by changing the type, e.g.
- The types won't automatically stay in sync with your database, so make sure to regenerate your types after your make changes to your database.
After you have generated your types, you can use them in your TypeScript projects:
One way to keep your type definitions in sync with your database is to set up a GitHub action that runs on a schedule.
Add a script to your package.json to generate the types:
In your repo, create the file
.github/workflows/update-types.yml. Add the following snippet into this file to define the action. If there is a change to your definitions, this script will commit the change to your repo.