porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.32k stars 265 forks source link

TypeScript issues with dynamic inserts #674

Closed noah-lee closed 11 months ago

noah-lee commented 1 year ago

I am getting what appears to be inconsistent TypeScript errors when writing dynamic insert queries:

Issue

// No errors:
const users = await sql<UserDto[]>`INSERT INTO users ${sql(payload)} returning *`;
// or
const helper = sql(payload, "username", "email", "password");
const users = await sql<UserDto[]>`INSERT INTO users ${helper} returning *`;

// Error: Type of operand must either be a valid promise or must not contain a callable then member.
const users = await sql<UserDto[]>`INSERT INTO users ${sql(payload, "username", "email", "password")} returning *`;

Expected I would expect all three above to be valid but the last one throws a TypeScript error.

Is this the intended behavior?

tsconfig

{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "target": "ES2022",
    "strict": true,
    "sourceMap": true,
    "outDir": "dist",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
}
alpharder commented 12 months ago

This shouldn't be an intended behavior, I've prepared a fix at https://github.com/porsager/postgres/pull/679