HeadlinesBriefing favicon HeadlinesBriefing.com

pg-typesafe: Zero-Config PostgreSQL TypeScript Typing

Hacker News: Front Page •
×

A developer has created pg-typesafe, a TypeScript tool that automatically generates types for PostgreSQL queries with zero runtime dependencies. The tool analyzes SQL queries and produces TypeScript definitions that catch type errors at compile time, addressing common pain points like INT4/INT8 serialization differences and nullable field detection.

Unlike existing solutions such as pgtyped or Kysely, pg-typesafe works with existing node-pg code without adding verbosity. The developer reports using it in a large project where it found several bugs and eliminated manual type definitions. The tool supports configuration options for handling BIGINTs as JavaScript bigints and typing JSONB columns based on context.

The tool is available as an npm package and includes command-line options for database connection, TypeScript configuration, and custom type transformations. While it requires SQL queries to be constants for type analysis, this limitation also prevents SQL injection vulnerabilities. The project is open source with TypeScript making up 63.4% of the codebase.