PgTyped is a code generator that enables typesafe SQL queries in TypeScript by automatically inferring types from a running PostgreSQL database.
The tool solves the problem of maintaining type safety when writing raw SQL in TypeScript applications. Rather than requiring developers to manually map database schemas to TypeScript types or use an ORM, PgTyped connects to a live Postgres instance and analyzes the actual schema to generate strictly typed query interfaces. It extracts SQL queries from both dedicated SQL files and TypeScript files, then produces corresponding TypeScript types and interfaces that guarantee parameter and result types match the database schema. The approach prevents SQL injection by sending queries and parameters separately to the database driver, allowing PostgreSQL itself to handle parameter substitution safely.
Developers should choose this tool if they prefer writing raw SQL while maintaining full type safety, and if their project uses PostgreSQL as the database. It suits teams that want to avoid the overhead of ORMs or schema definition layers but still need compile-time guarantees about query correctness. The tool supports watch mode for regenerating types as queries are written, works with both CommonJS and ESM, and provides helper utilities for complex parameter interpolation with arrays and objects.
The project maintains active development with ongoing API refinement. The maintainers welcome issue reports, feature requests, and pull contributions from the community.