Menu

1 / 9

typesql: write plain SQL strings, get exact TypeScript types (no codegen) : r/typescript

#typescript#reddittypescript#reddit#photo#ama
Reading 0:00
15s threshold

r/typescript

12h ago

u/ismail5412

typesql: write plain SQL strings, get exact TypeScript types (no codegen)

I created this new library that parses Postgres SQL inside TypeScript’s type system. You write a SQL string and the compiler infers the row and parameter types. It needs no codegen and no database connection.

const q = sql(`SELECT u.id, u.email, p.title FROM users u LEFT JOIN posts p ON p.author_id = u.id WHERE u.id = ${sql.param(7)}`); // { id: number; email: string; title: string | null }

sql("SELECT nme FROM users"); // error: column "nme" does not exist

Handles joins, CTEs, subqueries, window functions, INSERT/UPDATE/DELETE … RETURNING

Runs on pg or postgres.js, and can reuse your existing Kysely, Drizzle or Prisma schema

Requires TypeScript 7

https://github.com/ismailkarsli/typesql

Feedback welcome, especially queries that don’t type-check but should.

comcastbusiness

•

Ad

Get the most comprehensive cybersecurity for small business at the best price + ask about a $500 prepaid…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.