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:
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 type | TypeScript type |
|---|---|
SQLite INTEGER | number (throws SqlDecodeError outside ±2⁵³ and suggests -- @type bigint) |
SQLite REAL, TEXT, BLOB | number, string, Uint8Array |
SQLite ANY | number | bigint | string | Uint8Array |
Postgres int2, int4, float4, float8 | number |
Postgres int8 (and count) | bigint |
Postgres numeric | string (exact decimal text) |
Postgres bool | boolean |
Postgres text, varchar, uuid, interval, inet, cidr, macaddr, macaddr8, money, xml, tsvector, tsquery, bit, varbit, name, oid, char | string (exact server text) |
Postgres void (e.g. pg_notify(...)) | null |
Postgres json, jsonb | unknown until narrowed by an annotation or JSON shape |
Postgres timestamp, timestamptz | string (exact server text) |
| Postgres enum | String 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.
int8asnumber(Postgres): Set"types": { "int8": "number" }to map allint8columns andcount(*)tonumber; values outside JavaScript's safe integer range (±2⁵³) throw at runtime (int8 as number). Keep the defaultbigintif your IDs or counts can exceed 2⁵³ (int8 as bigint).timestampasDate(Postgres): Set"types": { "timestamp": "Date" }to decode and bindtimestampandtimestamptzcolumns as UTCDateobjects (timestamp as Date).- 64-bit
biginton SQLite: SQLite storesINTEGERvalues as 64-bit signed integers. Add-- @type biginton a column inschema.sqlto read and bind values beyond ±2⁵³ without precision loss (bigint). Without it,INTEGERmaps tonumberand 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
int8asnumber, ortimestamp/timestamptzasDate. - SQLite
INTEGERasboolean(expects0or1; any other value throws): integer booleans. - SQLite
INTEGERasbigint. - JSON text (
TEXTon SQLite,jsonorjsonbon 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:
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;// 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:
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.