SQL to TypeScript Interface Generator

Convert SQL CREATE TABLE statements to TypeScript interfaces with proper type mappings for PostgreSQL, MySQL, and SQLite.

What is SQL to TypeScript Interface Generator?

When a TypeScript backend or frontend needs to represent database rows as typed objects, writing interfaces by hand is error-prone and tedious — especially when schemas evolve. This converter reads your SQL CREATE TABLE statements and produces TypeScript interfaces with every column mapped to the correct type, nullable columns marked as optional with the ? modifier, and array types like TEXT[] rendered as string[]. You can choose whether temporal columns (TIMESTAMP, DATE, TIME) are represented as the Date constructor type for ORM models or as string for JSON API responses where dates arrive serialized. The output is clean, exportable, and ready to drop into any TypeScript project whether you are building type-safe API clients, ORM model layers, or database migration scripts.

How to Use

  1. Choose how temporal columns should be represented: Date for ORM and in-memory usage, or string for JSON API response types
  2. Toggle the export keyword on or off depending on whether you want the interfaces to be module-visible or declared within a namespace
  3. Paste one or more CREATE TABLE statements into the input area
  4. Click Generate TypeScript to produce the interface definitions
  5. Copy the output into a .ts file in your project

Why Use This Tool?

Covers over 30 SQL data types including PostgreSQL-specific ones like TIMESTAMPTZ, JSONB, UUID, and array types like TEXT[]
Nullable columns become optional properties with the ? modifier, matching TypeScript best practices for representing missing data
Each CREATE TABLE produces a separate named interface, so multi-table schemas generate a complete type suite in one pass
The Date vs string toggle lets you generate one set of interfaces for your ORM layer and another for your API layer without manual editing
JSON and JSONB columns map to Record<string, unknown> giving you a typed object without requiring an immediate custom interface

Tips & Best Practices

  • Use the Date → string option when generating types for API response payloads, since JSON serialization converts dates to ISO strings
  • Use the Date → Date option when generating types for ORM models where you work with JavaScript Date objects in application code
  • DECIMAL and NUMERIC map to number — for financial applications where precision matters, replace number with a branded type or decimal.js
  • JSON and JSONB map to Record<string, unknown> — replace this with a specific interface once you know the shape of your JSON data

Frequently Asked Questions

How are SQL types mapped to TypeScript types?

INTEGER, BIGINT, SERIAL, and all numeric SQL types map to number. VARCHAR, CHAR, TEXT, and UUID map to string. BOOLEAN maps to boolean. TIMESTAMP and DATETIME map to Date or string depending on your selection. DATE and TIME map to Date or string. JSON and JSONB map to Record<string, unknown>. BYTEA and BLOB map to Buffer. Array types like TEXT[] map to string[] and INTEGER[] maps to number[]. Columns without NOT NULL become optional with the ? modifier.

When should I avoid generating interfaces and write them manually instead?

If your application uses a query builder or ORM that already generates types (like Prisma Client, Drizzle, or TypeORM entities), adding hand-written interfaces creates a maintenance burden. Also, for highly dynamic schemas where columns are determined at runtime, static interfaces cannot capture the full shape and may be misleading.

How are nullable columns handled?

Columns that lack a NOT NULL constraint are generated as optional properties using the TypeScript ? modifier. For example, a column defined as age INTEGER (without NOT NULL) becomes age?: number rather than age: number. This means the property can be undefined at the type level.

Does the converter support PostgreSQL array types?

Yes. SQL array types like TEXT[], INTEGER[], and UUID[] are detected and mapped to their TypeScript array equivalents: string[], number[], and string[] respectively. The array suffix is recognized on any base SQL type.

Is my SQL schema transmitted to any server during conversion?

No. All parsing and code generation runs entirely in your browser. Your database schema is never sent over the network or stored on any external server.

Can I use these interfaces with an ORM like TypeORM or Prisma?

These interfaces represent the shape of database rows and are useful as standalone types. For TypeORM, you would typically define entity classes with decorators instead. For Prisma, the client generates its own types. However, these interfaces can complement ORM usage as DTO types for API layers or as parameters in repository pattern implementations.

Real-world Examples

Type-safe API client for a task management backend

A frontend application consumes a REST API backed by a PostgreSQL database. The developer generates TypeScript interfaces from the SQL schema to type API response objects, ensuring compile-time safety when accessing task properties.

ORM model types for a Node.js backend

A Node.js backend uses a lightweight query builder and needs typed model definitions. The developer generates interfaces with Date types for in-memory usage, then uses them as type parameters in repository functions.

Related Tools