Skip to main content
Version: 2.0.0
Last Updated: 2026-01-15
Purpose: Generate TypeScript types from Supabase database schema after migration.

Overview

TypeScript types provide type safety for database operations. This guide covers generating types using both CLI (recommended) and MCP approaches. Recommendation: Use CLI for type generation as it provides better error handling and integrates with build scripts.

Prerequisites

Verify Setup: MCP Check:
CLI Check:

Step 1: Set Project ID

Option A: Environment Variable
Option B: .env.local File
Note: Get project ID from mcp_supabase_get_project_url() or Supabase dashboard.

Step 2: Generate Types

Using Script:
Or Manual Command:

Step 3: Validate Generated Types

CLI:
Verify Content:

Method 2: Generate via MCP (Alternative)

Step 1: Generate Types

Step 2: Verify Output

The MCP will return generated types. You’ll need to save them to the types file manually. Note: CLI is preferred because:
  • Better error messages
  • Automatic file output
  • Integrates with scripts
  • More reliable for large schemas

Validation Checklist

After generating types, verify:
  • File size is substantial (>100KB for 465 tables)
  • npm run typecheck passes with 0 errors
  • No empty type definitions ([_ in never]: never)
  • All modules have type definitions:
Verify Module Types (MCP):
Expected Type Definitions:

Troubleshooting

Issue: Types file is empty or minimal

Symptoms: types.ts only contains empty type definitions or is very small Verify Database Has Tables (MCP):
Solutions:
  1. Verify project ID is correct
  2. Check Supabase CLI authentication: supabase login
  3. Try linking project: supabase link --project-ref <project-ref>
  4. Verify migrations are applied: mcp_supabase_list_migrations()

Issue: Type errors after generation

Symptoms: npm run typecheck shows errors Check Database Schema (MCP):
Solutions:
  1. Review error messages - may indicate schema changes needed
  2. Check for breaking changes in Supabase client library
  3. Update code to match new types
  4. If types are incorrect, restore backup and regenerate

Issue: Missing tables in types

Symptoms: Some tables don’t appear in generated types Verify Tables Exist (MCP):
Solutions:
  1. Verify tables exist in database
  2. Check table is in public schema (default for type generation)
  3. Regenerate types after ensuring all migrations are applied

Issue: CLI command fails

Symptoms: supabase gen types typescript returns error Check Connection:
Solutions:
  1. Re-authenticate: supabase login
  2. Re-link project: supabase link --project-ref <project-ref>
  3. Check network connectivity
  4. Try MCP as fallback

When to Regenerate Types

  • After applying new migrations
  • After schema changes in staging/production
  • Before major releases
  • When type errors appear in codebase
  • After migration validation completes

Backup Strategy

The generate script automatically backs up existing types before generating new ones:
  • Backup location: src/integrations/supabase/types.ts.backup.YYYYMMDD_HHMMSS
  • Restore: mv src/integrations/supabase/types.ts.backup.* src/integrations/supabase/types.ts

Integration with CI/CD

For automated type generation in CI:

Quick Reference



Last Updated: 2026-01-15
Version: 2.0.0