Typed Query Results
A typed query result is a TypeScript type for the rows a database driver returns. Drivers usually give any or a generic Row. You map that into a named type after you know the columns.
Detailed Explanation
client.query("select id, email from users") is a string to the engine. TypeScript does not parse SQL. The driver's TypeScript, if any, is typically Promise<any[]> or Promise<QueryResult<any>>. Some drivers let you pass a type argument (query<User>(sql)). That argument is an assertion. The database can return a number for id when you said string, and the type will not notice.
The honest sequence:
- Run the query.
- Treat each row as
unknown(or as the driver'sRowDataPacket/QueryResultRow). - Map columns explicitly:
id: String(row.id),emailchecked asstring. - Return
User[].
A helper function rows<T>(result: unknown): T[] without a mapper is the same assertion as query<T>. Put T on the output of a mapper function whose input is the driver row type.
Column names are the usual mismatch. SQL returns user_id. Your type wants id. The mapper is an adapter. Spread ({ ...row } as User) keeps user_id at runtime and lies in the type.
Nullability is the other mismatch. SQL NULL is null. Your domain type may use undefined or omit the key. strictNullChecks will catch string vs string | null if the driver types the column; many drivers do not. Generate types from the schema (Prisma, kysely, slonik + zod) when you can, so NULL columns become | null without a hand-written list.
Dynamic SQL (string concatenation, user-built where) cannot have a precise result type. If the column list is dynamic, the result is Record<string, unknown> until you pick a known variant. A discriminated union of result shapes, one per query name, is better than one Row for the whole app.
See SQL injection for why the query string should not include user data. This page is only the result type.
Code Examples
1. Explicit mapper from a driver row.
// 1. Explicit mapper from a driver row.
type DriverRow = Record<string, unknown>;
type User = { id: string; email: string };
function toUser(row: DriverRow): User | undefined {
if (typeof row.id !== "string" && typeof row.id !== "number") return undefined;
if (typeof row.email !== "string") return undefined;
return { id: String(row.id), email: row.email };
}2. A query helper that returns your type, not the driver type.
// 2. A query helper that returns your type, not the driver type.
async function listUsers(query: (sql: string) => Promise<DriverRow[]>): Promise<User[]> {
const raw = await query("select id, email from users");
const out: User[] = [];
for (const row of raw) {
const user = toUser(row);
if (user) out.push(user);
}
return out;
}3. NULL columns must appear in the type.
// 3. NULL columns must appear in the type.
type UserRow = { id: string; nickname: string | null };
function displayName(row: UserRow): string {
return row.nickname ?? "none";
}Common Gotchas
`query<User>(sql)` does not parse SQL
it is `as User[]` with extra steps. Map or generate.
Spread-plus-assert keeps extra columns and the wrong names
build a literal.
Driver `any` will infect the app if you return it
the repository / mapper is the last line of defense.
A dynamic column list is `Record<string, unknown>`
do not pretend it is `User`.