SQL to TypeScript Interface Generator
Bridging the gap between relational databases and TypeScript has always been a source of friction for developers. While modern tools like Drizzle ORM and Prisma have excellent type safety, working with raw queries, legacy databases, or lightweight query builders like Kysely often requires manual synchronization. Manually translating SQL `VARCHAR` or `TIMESTAMP` into TypeScript `string` and `Date` is tedious and prone to human error, especially as your schema evolves.
The SQL to TypeScript Generator automates this workflow entirely in your browser. By pasting your `CREATE TABLE` statements, you can instantly scaffold accurate TypeScript interfaces or type aliases. It intelligently handles database-specific quirks like nullable columns, converts `snake_case` to `camelCase`, and even preserves your original SQL types as JSDoc comments—ensuring your application code remains perfectly aligned with your database structure.
Generation Options
This tool runs 100% in your browser; your data never leaves your device. Privacy details
Why SQL Schemas and TypeScript Types Must Stay in Sync
In modern web development, type safety is paramount. When your application layer is written in TypeScript, but your database is defined in SQL, a "type gap" emerges. If a column is added, renamed, or changes type in the database, the TypeScript code often remains unaware until a runtime error occurs. Synchronizing these two worlds ensures that your API layers, data access objects, and frontend components always consume the correct shapes.
Manually maintaining these types is a significant bottleneck. A formatted SQL table with dozens of columns requires painstaking effort to convert. This generator eliminates that busywork, allowing developers to treat their SQL DDL as the single source of truth for bootstrapping their TypeScript models.
PostgreSQL to TypeScript: Type Mapping Reference
Different SQL dialects use varied terminology for data types. This tool automatically maps them to the appropriate TypeScript primitives:
- String Types: `VARCHAR`, `TEXT`, `CHAR`, `UUID`, and `CITEXT` are mapped to TypeScript's `string`.
- Numeric Types: `INT`, `BIGINT`, `SMALLINT`, `SERIAL`, `FLOAT`, `DOUBLE`, and `DECIMAL` are mapped to `number`.
- Booleans: `BOOLEAN` and `BOOL` become `boolean`.
- Dates: `DATE`, `TIMESTAMP`, and `TIMESTAMPTZ` are mapped to `Date`.
- JSON: PostgreSQL's powerful `JSON` and `JSONB` types are mapped to `Record<string, unknown>`, providing a safe starting point for structured data.
By retaining the original SQL types via the JSDoc option, developers can always refer back to the exact database constraint without leaving their code editor.
Nullable Columns: | null vs Optional Fields in TypeScript
Handling SQL `NULL` values in TypeScript requires careful consideration. A database column without a `NOT NULL` constraint can legally contain a null value. In TypeScript, this can be modeled in two primary ways:
1. Union Types (`| null`): This enforces that the key must exist on the object, but its value can be null. This is often the safest representation of a database row retrieved from a query.
2. Optional Fields (`?`): This implies the key might not be present on the object at all. This is useful for insert statements where a default value will be provided by the database if the field is omitted.
The tool provides options for both approaches, as well as a combined approach (`fieldName?: Type | null`), allowing you to tailor the output to your specific ORM or validation library.
snake_case to camelCase: Handling the Naming Convention Gap
Databases overwhelmingly favor `snake_case` for table and column names, while JavaScript and TypeScript standardly use `camelCase`. This discrepancy forces developers to either write awkward TypeScript code with underscores or implement mapping layers in their data access objects.
With the built-in camelCase conversion, this tool instantly bridges that gap. A database column like `created_at` becomes `createdAt` in your TypeScript interface. When combined with tools like JSON Schema to TypeScript, you can ensure a consistent naming convention throughout your entire tech stack.
Databases overwhelmingly favor snake_case for table and column names, while JavaScript and TypeScript standardly use camelCase. This discrepancy forces developers to either write awkward TypeScript code with underscores or implement mapping layers in their data access objects.
With the built-in camelCase conversion, this tool instantly bridges that gap. A database column like created_at becomes createdAt in your TypeScript interface. When combined with tools like JSON Schema to TypeScript, you can ensure a consistent naming convention throughout your entire tech stack.
Using Generated Types with Drizzle, Kysely, and Knex
While heavyweight ORMs generate their own client code, micro-ORMs and query builders rely heavily on developer-provided types:
- Kysely: Requires explicitly defined TypeScript interfaces for every table to provide its signature type-safe query building.
- Knex.js: Allows you to pass generic types to its query builder (e.g.,
knex<User>('users')) for better intellisense.
Keeping Types Updated as Your Schema Evolves
As your application grows, your database schema will inevitably change. Iterative development means running migrations and updating types in tandem. Whenever you alter a table, simply paste the updated CREATE TABLE syntax into the generator to refresh your TypeScript interfaces.
If you are also working with JSON data, you might find our JSON to TypeScript tool or the SQL to JSON Schema generator helpful for maintaining consistency across different serialization formats. For generating test fixtures, the Mock Data Generator is an excellent companion.
How to Use the SQL to TypeScript Interface Generator
- [object Object]
- [object Object]
- [object Object]
- [object Object]
- [object Object]
Common Use Cases
- [object Object]
- [object Object]
- [object Object]
- [object Object]
- [object Object]
- [object Object]
Frequently Asked Questions
How are SQL nullable columns represented in TypeScript?
By default, SQL columns without a NOT NULL constraint are treated as nullable. The generator allows you to choose how these are represented: either by appending `| null` to the type, making the field optional with a `?`, or both (e.g., `fieldName?: type | null`).
How does this handle PostgreSQL-specific types like JSONB?
PostgreSQL JSON and JSONB types are mapped to `Record<string, unknown>` in TypeScript. UUIDs and CITEXT are mapped to `string`. PostgreSQL array types are mapped to `any[]` or `Type[]` depending on the column configuration.
What is the difference between interface and type in TypeScript?
Both interfaces and type aliases can be used to describe database models. Interfaces are slightly better for object-oriented patterns and declaration merging, while type aliases are more flexible for unions and intersections. The tool lets you generate either based on your project's coding standards.
How are snake_case column names converted to camelCase?
When the camelCase option is enabled, the tool scans for underscores followed by a letter (e.g., `user_id`) and converts it to camelCase (`userId`). This bridges the gap between typical database naming conventions and JavaScript/TypeScript standards.
Can I use the output with an ORM like Prisma or Drizzle?
Yes, generated types are excellent for Drizzle ORM, Kysely, or raw SQL queries (like pg or mysql2). Prisma generates its own types, but you can use this tool to quickly draft TypeScript models before writing your Prisma schema.
How are SERIAL/auto-increment columns handled?
SERIAL or AUTO_INCREMENT primary keys are always mapped to `number`. If you enable the 'Generate Insert Types' option, the tool creates a separate type where these auto-generated columns are marked as optional, making inserts type-safe.
What about composite primary keys?
The simple parser looks for `PRIMARY KEY` at the column level. Table-level constraints (like `PRIMARY KEY (id1, id2)`) are ignored in the generated types, but the individual columns will still be correctly parsed into TypeScript properties.
Related Tools
SQL Formatter
Format and beautify SQL queries entirely in your browser. No database schema uploaded.
JSON to TypeScript
Generate TypeScript interfaces from JSON API responses locally and securely.
JSON Schema to TypeScript
Convert JSON Schema Draft definitions into TypeScript interfaces instantly. Local browser-only parsing, zero data uploads.
SQL to JSON Schema Generator
Convert SQL CREATE TABLE statements into JSON Schema definitions automatically in your browser.
Mock Data Generator
Generate realistic fake data (names, emails, addresses, UUIDs) locally in your browser. Export to JSON, CSV, or SQL INSERT.