SQL to TypeScript interface converter
Convert supported PostgreSQL CREATE TABLE definitions into TypeScript row interfaces with explicit nullability and safe exact numeric representations.
How it works
Paste one or more PostgreSQL CREATE TABLE statements. The parser supports optional IF NOT EXISTS, optional schema names, quoted identifiers, built in column types, precision, defaults, null constraints, primary keys, and common constraints.
Review the generated interface for each table. Nullable columns remain required properties with a `| null` value type. The JSDoc option adds sanitized default expressions and primary key notes.
Check the output against your query shape and driver configuration. A query that selects only some columns has a different shape from the full table row represented here.
Use the output as a starting point for application types. Add runtime validation and adjust driver specific representations in your project.
Step by step
- Paste supported PostgreSQL table definitions
- Review generated row properties and value types
- Check query shapes and driver representations
Worked example
Example input: CREATE TABLE finance.positions (id bigint PRIMARY KEY, ticker varchar(12) NOT NULL, shares numeric(12, 4) NOT NULL, note text DEFAULT 'created, reviewed', metadata jsonb);
The generated row type includes id and ticker as required strings, shares as a required string, note as a required nullable string, and metadata as a required nullable unknown value. The bigint and numeric fields use strings to avoid assuming that JavaScript numbers preserve exact database values.
When to use this converter
Use this converter to turn supported PostgreSQL table definitions into a starting TypeScript type for full database rows. It keeps nullable values distinct from missing properties.
Review type mappings against your driver and query results before using the generated interface in an application.
Common mistakes
Do not treat a nullable column as an optional property. A selected database row includes the column, and its value may be null.
Do not assume the driver returns numeric and bigint values as strings. Configure or convert driver results to match the generated interface.
Parsing and type rules
For each supported table, split definitions at commas outside parentheses and quoted values. For each column, map the SQL type to a TypeScript type. Emit a required property, adding `| null` when the database column allows null. Primary key columns are nonnullable. Defaults are documentation only.
This tool generates row interfaces from a bounded PostgreSQL CREATE TABLE subset. Exact numeric and bigint values are represented as strings. That type assumes the application configures or converts driver results accordingly. Unknown SQL types map to unknown. JSON and JSONB also map to unknown.
Scope and limitations
This is a bounded PostgreSQL CREATE TABLE parser, not a complete SQL parser. It supports basic table definitions, schema qualified and quoted names, nested expressions, common column constraints, defaults, and primary keys. Unsupported statements or table options return an error without partial interfaces. It does not handle ALTER TABLE, views, CREATE TABLE AS, partitioning, inheritance, or every PostgreSQL grammar form. Numeric and bigint values are emitted as strings and require driver configuration or conversion. Dates are strings by assumption. Unknown types map to unknown. Generated row properties represent full rows, not optional insert fields or partial query results. Generated types do not validate data at runtime.
Key terms
- Row interface
- A database row interface describes the properties returned for a complete row, including nullable values.
- Nullable column
- A nullable column permits a null value. It does not mean that the property is absent from a full row.
- Primary key
- A primary key uniquely identifies rows and its columns cannot contain null values in PostgreSQL.
Related tasks
Convert a supported PostgreSQL table definition into TypeScript row types. Use a separate API schema tool when you need to describe HTTP requests and responses.
This converter focuses on database row shapes. It does not create runtime validation code or database access functions.
FAQ
Which SQL statements can it read?
The parser reads supported CREATE TABLE statements and splits column and constraint definitions only at top level. It keeps commas inside type precision, nested expressions, and quoted defaults together. Column names, types, nullability, defaults, and primary keys are used to build one interface per table.
How are null columns and primary keys represented?
A nullable database column is still present in a selected row, so its property is required and its value type includes null. A NOT NULL column and a primary key do not include null. A default affects inserts, not whether a selected row contains the property.
How are SQL types mapped to TypeScript?
Small integer and floating point types map to number. Bigint, numeric, decimal, and money map to string to preserve exact values. Configure your database driver to return those values as strings or convert them before using the generated type. JSON and JSONB map to unknown because JSON can contain a scalar, array, object, or null. Date and time types map to string.
What syntax is outside the supported subset?
The parser supports a bounded PostgreSQL CREATE TABLE form with optional IF NOT EXISTS, optional schema qualification, quoted identifiers, column definitions, common constraints, defaults, and primary keys. It rejects unsupported suffixes and unrelated statements instead of returning partial output. It does not parse ALTER TABLE, views, CREATE TABLE AS, partitions, inheritance, or every PostgreSQL type and constraint form.
What does the JSDoc option add?
When enabled, JSDoc adds a safe summary for a column default expression and primary key status. Newlines are flattened and comment terminators are escaped so SQL input cannot close the generated comment. The option changes documentation only, not the TypeScript property types.
Related tools
Use database row interfaces alongside a separate API schema and runtime validation layer when your application needs those contracts.