0
Challenge · 45Schema as Code

Generated types & the typed client

Toolingused in ~90% of apps

Six months ago someone renamed `posts.body` to `posts.summary`. The migration shipped fine. Three different call sites that still say `.select('body')` didn't — they compiled without a single complaint, then failed in production with a PostgREST 400 the instant a user hit them. TypeScript didn't do its one job, not because it's weak, but because nobody told it the schema had changed. That's the whole concept: a generated types file is a snapshot, and it protects you only for as long as it's kept current.

Core idea. `supabase gen types typescript` reads your live schema and emits a `Database` type describing exactly three shapes per table — `Row` (what a SELECT returns), `Insert` (what you may supply, with defaulted/nullable/identity columns made optional), and `Update` (everything optional, since a partial update supplies only what's changing). Pass that `Database` type into `createClient<Database>(...)` and PostgREST calls become checked against real column names at compile time — but the whole guarantee only holds as long as the committed file is regenerated every time the schema changes.
Playground, this isn't a screenshot
-- current schema --
Schema (Postgres)
create table likes (
  post_id bigint not null,
  user_id uuid   not null,
  primary key (post_id, user_id)
);
database.types.ts
likes: {
  Row: {
    post_id: number
    user_id: string
  }
  Insert: {
    post_id: number
    user_id: string
  }
}
Sync status
in sync
1 / 4
likes matches database.types.ts. Everyone's compiler agrees with the database.

This is the starting state: the committed types file describes exactly the columns that exist.

↑ play a little first, then dig in