Generate TypeScript types from Supabase database schema
Generates TypeScript types from Supabase database schema for type-safe database queries.
/plugin marketplace add citadelgrad/scott-claude-code/plugin install scott-claude-code@scott-claude-codeclaude-sonnet-4-5supabase/Generate TypeScript types from the Supabase database schema.
Run the following command to generate types:
npx supabase gen types typescript --project-id YOUR_PROJECT_ID > lib/database.types.ts
Or if using local Supabase:
npx supabase gen types typescript --local > lib/database.types.ts
{
"scripts": {
"gen-types": "npx supabase gen types typescript --project-id $SUPABASE_PROJECT_ID > lib/database.types.ts",
"gen-types:local": "npx supabase gen types typescript --local > lib/database.types.ts"
}
}
import type { Database } from '@/lib/database.types'
// Get table type
type User = Database['public']['Tables']['users']['Row']
type UserInsert = Database['public']['Tables']['users']['Insert']
type UserUpdate = Database['public']['Tables']['users']['Update']
// Use with Supabase client
const supabase = createClient<Database>(url, key)
// Type-safe queries
const { data } = await supabase
.from('users')
.select('*')
.single() // data is typed as User
// Type-safe inserts
const { data } = await supabase
.from('users')
.insert({
email: 'user@example.com',
name: 'John Doe'
}) // TypeScript validates the shape
// lib/database.helpers.ts
import type { Database } from './database.types'
// Extract table types
export type Tables<T extends keyof Database['public']['Tables']> =
Database['public']['Tables'][T]['Row']
export type Enums<T extends keyof Database['public']['Enums']> =
Database['public']['Enums'][T]
// Usage
import type { Tables } from '@/lib/database.helpers'
type User = Tables<'users'>
type Post = Tables<'posts'>
Run npm run gen-types after:
lib/ or types/any instead of generated types# .husky/pre-commit
#!/bin/sh
npm run gen-types
git add lib/database.types.ts
Issue: supabase command not found
npm install -g supabase
Issue: Missing project ID
# Find your project ID in Supabase dashboard
# Or set in .env
SUPABASE_PROJECT_ID=your-project-id
Issue: Types not updating
# Clear cache and regenerate
rm lib/database.types.ts
npm run gen-types
Generate and use TypeScript types to catch database-related bugs at compile time instead of runtime.