> ## Documentation Index
> Fetch the complete documentation index at: https://supaschema.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# types

> Generate TypeScript interfaces and Zod validators directly from PostgreSQL schema files.

Use `types` when application code needs the latest database shape before a migration is applied.

`types` reads one schema source, writes `database.types.ts`, and writes `database.zod.ts` unless TypeScript output is sent to stdout.

`database.types.ts` follows the Supabase generated type contract: `Database`, helper types such as `Tables`, `TablesInsert`, `TablesUpdate`, `Enums`, `CompositeTypes`, and runtime enum `Constants`. `database.zod.ts` is a Supaschema runtime extension exposed as `SupaschemaZod`; it is not part of the upstream Supabase TypeScript contract.

`SupaschemaZod` translates only CHECK constraints whose refinements are loose-or-exact for the client-to-PostgreSQL wire path. Scalar integer and float8/double precision columns support bounds; typmod-free numeric/decimal supports non-strict bounds; all three support `BETWEEN`. Equality is limited to safe integer literals on integer-family columns, and parsed float literals produce only non-strict bounds. Typmod-free text, varchar/character varying, and citext support `char_length`; typmod-free text and varchar/character varying support `IN` only without an explicit collation. Bare booleans become column refinements. Numeric column comparisons are null-aware and require wire-exact small-integer or float8 targets. Casts unwrap only when both the source and safe text-family target are typmod-free and have the same resolved base type. Scalar columns inherit column-level checks from every unambiguously resolved `CREATE DOMAIN` in their type chain, with `VALUE` resolved to the column. Real/float4, typmod-constrained numeric, char/bpchar, domain arrays, ambiguous domain hops, explicitly collated list checks, widening or typmod-bearing casts, precision-sensitive arbitrary-numeric or bigint comparisons, and any other uncertain expression are skipped rather than approximated. `NOT VALID` table checks refine `Insert` and `Update` but not `Row`, because existing rows are unverified until the constraint is validated.

The generated contract is derived from the schema source itself. Tables, views, materialized views, view dependencies, functions, enums, composites, and supported catalog-backed objects should resolve from the parsed SQL model before a database is available. If a view depends on an extension-owned relation that is not in the configured source, add or use a supported extension model/source rather than patching application code with local casts or copied contracts.

If a modeled relation, function, extension, or expression should resolve but is generated as `unknown`, fix the schema source, supported model, or typegen diagnostic path before updating application consumers. Unsupported PostgreSQL scalars intentionally keep the upstream `unknown` fallback.

## Use this when

* A pull request changes SQL and TypeScript together.
* You want generated runtime validators at API boundaries.
* A database connection is not available.
* The schema tree changed and generated contracts need refresh.

## Run it

```bash theme={null}
npx supaschema types
npx supaschema types --from dir:database/schemas
npx supaschema types --from migrations:supabase/migrations
npx supaschema types --out src/lib/database.types.ts
npx supaschema types --out stdout
```

`types` is the generated-contract command. `diff` writes migration SQL; it does not refresh TypeScript or Zod outputs.

## Flags

<ParamField path="--from" type="source">
  Schema source to parse. Defaults to the configured schema tree.
</ParamField>

<ParamField path="--out" type="path | stdout">
  TypeScript output path. `stdout` prints types and skips the Zod file.
</ParamField>

## Output paths

| Config key           | Default                                                             |
| -------------------- | ------------------------------------------------------------------- |
| `typesFile`          | `database.types.ts`                                                 |
| `zodFile`            | `database.zod.ts`                                                   |
| `zodTypesImportPath` | derived from `typesFile` during joint generation; otherwise omitted |

Commit generated outputs when reviewers should see app-type impact in the same pull request as schema changes.

## Related

<CardGroup cols={2}>
  <Card title="Type generation guide" icon="book-open" href="/docs/guides/generate-supabase-types-without-database">
    Generate types before applying migrations.
  </Card>

  <Card title="Configuration" icon="sliders-horizontal" href="/docs/configuration/config-file">
    Set output paths and workflow policy.
  </Card>

  <Card title="Diff" icon="file-diff" href="/docs/commands/diff">
    Generate migration SQL from schema changes.
  </Card>

  <Card title="ORM-free apps" icon="database" href="/docs/concepts/orm-free-applications">
    Use generated types and validators as the application contract.
  </Card>
</CardGroup>
