Skip to content

Types ​

Every column and parameter in a query gets a TypeScript type—automatically from your database via hyper-sql pull (stored in the lock file), or customized with an annotation.

For example, SQLite has no native boolean type and stores booleans as 0 or 1. Adding -- @type boolean to the column in schema.sql converts it automatically in every query:

sql
CREATE TABLE books (
  in_print INTEGER NOT NULL -- @type boolean
) STRICT;

Without the annotation, in_print is typed as number. With it, in_print becomes boolean everywhere. See integer-booleans.

Default type mapping ​

Database typeTypeScript type
SQLite INTEGERnumber (throws SqlDecodeError outside ±2⁵³ and suggests -- @type bigint)
SQLite REAL, TEXT, BLOBnumber, string, Uint8Array
SQLite ANYnumber | bigint | string | Uint8Array
Postgres int2, int4, float4, float8number
Postgres int8 (and count)bigint
Postgres numericstring (exact decimal text)
Postgres boolboolean
Postgres text, varchar, uuid, interval, inet, cidr, macaddr, macaddr8, money, xml, tsvector, tsquery, bit, varbit, name, oid, charstring (exact server text)
Postgres void (e.g. pg_notify(...))null
Postgres json, jsonbunknown until narrowed by an annotation or JSON shape
Postgres timestamp, timestamptzstring (exact server text)
Postgres enumString literal union of its enum labels from sql.lock
Postgres array(T | null)[] (Postgres doesn't guarantee non-null elements; narrow a column to T[] with -- @col to enforce non-null elements at runtime). Array parameters accept the same type.

Nullable columns map to T | null. Even a NOT NULL column can become nullable in a specific query—for example on the outer side of a LEFT JOIN. See What hyper-sql infers for the full list of cases.

If a column uses a database type that isn't in this table (such as Postgres geometry), hyper-sql's mapper reports an error until you give it a -- @type <type> annotation in schema.sql.

Project-wide type options ​

Use the types setting in your project options to change int8 or timestamp mapping across your entire project. For a single column, use a -- @type annotation instead.

  • int8 as number (Postgres): Set "types": { "int8": "number" } to map all int8 columns and count(*) to number; values outside JavaScript's safe integer range (±2⁵³) throw at runtime (int8 as number). Keep the default bigint if your IDs or counts can exceed 2⁵³ (int8 as bigint).
  • timestamp as Date (Postgres): Set "types": { "timestamp": "Date" } to decode and bind timestamp and timestamptz columns as UTC Date objects (timestamp as Date).
  • 64-bit bigint on SQLite: SQLite stores INTEGER values as 64-bit signed integers. Add -- @type bigint on a column in schema.sql to read and bind values beyond ±2⁵³ without precision loss (bigint). Without it, INTEGER maps to number and throws on unsafe values.

Overriding a single column ​

Add -- @type T (or -- @type T from './file.ts') on the line where a column begins in schema.sql to type that column across every query. Annotations must be compatible with what the database type actually proves:

  • The base type itself, a branded version of it, or a union of string/number literals (verified at runtime on every read): SQLite annotations, Postgres annotations.
  • Postgres int8 as number, or timestamp/timestamptz as Date.
  • SQLite INTEGER as boolean (expects 0 or 1; any other value throws): integer booleans.
  • SQLite INTEGER as bigint.
  • JSON text (TEXT on SQLite, json or jsonb on Postgres) as any TypeScript type with a runtime type guard: -- @type Settings check isSettings from './settings.ts': SQLite JSON check, Postgres JSON check.
  • Postgres enums are typed automatically and need no annotation: enums.

Incompatible annotations fail at compile time right on the annotation line. -- @col and -- @param overrides follow the same compatibility rules.

SQLite schema.sql example:

sql
CREATE TABLE books (
  isbn TEXT PRIMARY KEY, -- @type Isbn from './src/ids.ts'
  title TEXT NOT NULL,
  binding TEXT NOT NULL, -- @type 'paperback' | 'hardcover'
  in_print INTEGER NOT NULL, -- @type boolean
  copies INTEGER NOT NULL, -- @type bigint
  profile TEXT NOT NULL -- @type AuthorProfile check isAuthorProfile from './src/profile.ts'
) STRICT;
ts
// src/ids.ts & src/profile.ts
export type Isbn = string & { readonly brand: 'Isbn' }
export interface AuthorProfile { readonly website: string | null; readonly genres: readonly string[] }
export const isAuthorProfile = (v: unknown): v is AuthorProfile =>
  typeof v === 'object' && v !== null && 'website' in v && (v.website === null || typeof v.website === 'string') &&
  'genres' in v && Array.isArray(v.genres) && v.genres.every((g) => typeof g === 'string')

Postgres schema.sql example:

sql
CREATE TABLE flights (
  number text PRIMARY KEY,
  status text NOT NULL, -- @type 'scheduled' | 'delayed' | 'cancelled'
  distance_km bigint NOT NULL, -- @type number
  departs timestamptz NOT NULL -- @type Date
);

Arrays ​

Postgres array columns decode as (T | null)[]. If you know an array column never contains NULL elements, narrow it with -- @col name: T[]—any unexpected NULL element will then throw at runtime. See arrays.

Array parameters don't need annotations: assigning or comparing against a text[] column, or casting like :ids::int8[], automatically types the parameter as (T | null)[] and binds it as a Postgres array literal. null elements bind as SQL NULL, while the literal string "NULL" stays quoted text. This works for numbers, strings, booleans, and enum labels. Arrays of JSON—or arrays of timestamps when "timestamp": "Date" is enabled—aren't supported directly; pass them as :p::text[] and cast in SQL. See array parameters.

To filter by a list of values, use col = ANY(:list), which types :list as T[] matching the column. See any-list.