Skip to content

Installation ​

Requirements ​

  • TypeScript 7.1 or later. This is the native compiler; 7.1.0-beta works. You can also use tsrs (a Rust port of TypeScript 7.1). Older versions are not supported: TypeScript 7.0, 6 and 5.9 may still type-check queries, but they are not tested and the peer dependency rejects them. TypeScript 6 and 5.9 are several times slower, and editor completions need 7.1.
  • strictNullChecks. Set "strict": true (or at least "strictNullChecks": true) in tsconfig.json. Without it null and undefined belong to every type, so TypeSQL cannot tell nullable columns from others: every sql() call and .add() of a migration is then an error that says TypeSQL requires strictNullChecks. With "skipLibCheck": false, TypeScript also reports errors inside TypeSQL's own declaration files (TS2344, TS2589) until strictNullChecks is on.
  • A supported database. TypeSQL supports PostgreSQL and SQLite. MySQL support is planned.

Install TypeSQL ​

Install @typesql/core and the adapter for your driver:

PackageRuns queries on
@typesql/pgnode-postgres (pg)
@typesql/postgrespostgres.js (postgres)
@typesql/sqlitenode:sqlite, bun:sqlite or better-sqlite3

@typesql/core has no runtime dependencies (only typesql migrate needs typescript at runtime; see tracked migration files). The packages are ESM-only. import and Node's require() of ES modules both work, but TypeScript rejects imports from .cts files under node16/nodenext resolution (TS1479). The adapter types rows the way your driver returns them and runs queries on your client.

Install TypeScript 7.1 ​

If your project is on an older TypeScript, add 7.1 as a dev dependency:

sh
npm install -D typescript@7.1.0-beta
sh
pnpm add -D typescript@7.1.0-beta
sh
yarn add -D typescript@7.1.0-beta

Your editor should check the project with TypeScript 7.1 as well. VS Code extension shows how to point VS Code at it. You can also use tsrs, a Rust port of TypeScript 7.1.

Using Kysely, Drizzle or Prisma ​

If your schema is already described for one of them, install its TypeSQL package instead of writing it again:

PackagePeer dependency
@typesql/kyselykysely 0.28 or later
@typesql/drizzledrizzle-orm 0.45 or later, before 1.0
@typesql/prisma@prisma/client 7 or later

See From Kysely, Drizzle or Prisma.

The typesql command (optional) ​

@typesql/cli provides typesql introspect, which writes your schema from a PostgreSQL or SQLite database. See From a live database. It also provides typesql migrate for tracked migration files, which import their types from @typesql/core: keep @typesql/core a dependency of your project, as above, even though @typesql/cli depends on it too (pnpm doesn't let your files import it through the CLI).

Editor extension (optional) ​

The VS Code extension, and @typesql/language-server in Neovim, complete table and column names inside your SQL strings and highlight the SQL. Types and errors work without them.

Next ​

Continue with the Quick start.

Upgrading from an earlier version? See Upgrading.