sql to typescript interface converter
typetools
instant, client-side type converter • 0ms network latency
export interface RootPayload {
id: string;
username: string;
email: string;
is_active: boolean;
login_count: number;
account_balance: string | null;
last_login_at: globalThis.Date | number | null;
created_at: globalThis.Date | number;
}step-by-step integration guide
technical specification differences & end-to-end conversion workflow
PostgreSQL SELECT rows with node-postgres
This converter reads one PostgreSQL CREATE TABLE statement and emits a row interface for SELECT results using node-postgres (pg 8, default text parsers). It does not generate INSERT types. Other database drivers, custom parsers, binary mode, and JSON-serialized rows can have different runtime types.
Columns, precision and nullability
One-line and multiline SQL use the same token parser. Parenthesized precision, quoted identifiers, comments and DEFAULT expressions are preserved without splitting embedded commas. DEFAULT expressions are not evaluated. INT/SMALLINT/SERIAL and floating types map to number; BIGINT/BIGSERIAL and NUMERIC/DECIMAL map to string to preserve precision; text and UUID map to string; BOOLEAN maps to boolean. DATE/TIMESTAMP/TIMESTAMPTZ map to globalThis.Date | number: finite dates are Date instances, and PostgreSQL infinite dates can be numeric infinities. TIME/TIMETZ remain strings. JSON/JSONB map to unknown because JSON values may be objects, arrays, scalars or null.
With strict nulls on, columns allow | null unless NOT NULL, SERIAL, or an inline/table PRIMARY KEY makes them non-nullable. This includes composite primary keys. All SELECT fields remain required; infer optional does not affect SQL rows. Strict nulls off intentionally omits nullable unions. SQL DEFAULT does not make a SELECT property optional or non-nullable.
Supported scope and explicit errors
Unsupported types (including arrays, enums and custom types), foreign keys, CHECK/generated clauses, dialect-specific syntax, trailing statements and table options fail with a specific error. Nothing is silently skipped. Remove unsupported clauses or convert a supported table definition separately. UNIQUE is accepted but adds no TypeScript constraint. JavaScript numbers and Date instances retain the driver's precision limitations: floating-point decimals are approximate; Date truncates microseconds to milliseconds and DATE/TIMESTAMP depend on the Node process timezone. See the official node-postgres type documentation and pg default text parsers.
frequently asked questions
common developer questions answered
q:Why are nullable SQL columns typed as 'T | null' instead of optional 'field?: T'?
For SELECT row types every column key is required. With strict nulls on, nullable database values are represented as T | null. NOT NULL, SERIAL and inline or table PRIMARY KEY constraints remove that union. Infer optional does not change SQL row types.
q:How are PostgreSQL array columns (e.g., TEXT[] or INT4[]) converted to TypeScript?
PostgreSQL array types currently fail with an explicit unsupported-syntax error. They are never silently flattened into scalar fields. Convert these fields manually using the parser configuration of your driver.
q:How should PostgreSQL BIGINT and DECIMAL columns be typed in TypeScript?
Under node-postgres default text parsing, BIGINT and NUMERIC/DECIMAL map to string, avoiding JavaScript numeric rounding. Custom parsers and other drivers require corresponding adjustments.