askill
types-gen

types-genSafety --Repository

Generate TypeScript types from Supabase database schema

0 stars
1.2k downloads
Updated 2/3/2026

Package Files

Loading files...
SKILL.md

Generate Supabase Types

Generate TypeScript types from the Supabase database schema.

Command

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

Setup for Auto-Generation

1. Add to package.json

{
  "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"
  }
}

2. Usage in Code

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

3. Create Utility Types

// 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'>

4. When to Regenerate

Run npm run gen-types after:

  • Creating new tables
  • Adding/removing columns
  • Changing column types
  • Modifying RLS policies
  • Adding enums

5. Best Practices

  • Commit generated types to git
  • Run after schema changes
  • Use in all Supabase queries
  • Create helper types for common patterns
  • Keep types file in lib/ or types/
  • Don't manually edit generated file
  • Don't use any instead of generated types

6. Integration with Pre-commit Hook

# .husky/pre-commit
#!/bin/sh
npm run gen-types
git add lib/database.types.ts

Troubleshooting

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.

Install

Download ZIP
Requires askill CLI v1.0+

AI Quality Score

AI review pending.

Metadata

Licenseunknown
Version-
Updated2/3/2026
Publishergpaura

Tags

database