Generating TypeScript Types from Supabase for Type-Safe Apps

Learn how to automatically generate TypeScript types from your Supabase database schema to keep your app type-safe and bug-free.

User Avatar

Faisal Husain

When working with Supabase and TypeScript, keeping your types in sync with your database is essential for type safety. Here’s a simple 2-step guide to generate types from your Supabase project.

Step 1: Get Your Supabase Access Token

  1. Go to your Supabase Dashboard.
  2. Click on your profile icon (top right) and select Account.
  3. Scroll down to Access Tokens.
  4. Click Generate New Token, give it a name (e.g., "typegen"), and copy the generated token.

Step 2: Generate Types Using the Supabase CLI

  1. Open your terminal.
  2. Log in with your access token:
Terminal
supabase login --token YOUR_PASTED_TOKEN
  1. Generate the types (replace "projectid" with your actual project ref):
Terminal
npx supabase gen types typescript --project-id **projectid** > lib/database.types.ts --debug
  1. Additionally you can write a script in package.json.
package.json
{
  "scripts": {
    "generate-types": "npx supabase gen types typescript --project-id **projectid** > lib/database.types.ts --debug"
  }
}

Conclusion:
That’s it! You now have up-to-date TypeScript types for your Supabase schema. Use these types in your app for a safer and smoother development experience.

Want to hire me as a freelancer? Let's discuss.
Drop a message and let's discuss
HomeAboutProjectsBlogsHire MeCrafts