postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Typing with inserts

Open noslouch opened this issue 1 year ago • 3 comments

Hi there. What a lovely library. I'm encountering a typing issue with dynamic inserts: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.

const users = [{
  name: 'Murray',
  age: 68,
  garbage: 'ignore'
},
{
  name: 'Walter',
  age: 80
}];

await sql`insert into users ${sql(users, 'name', 'age')}`; // fine
await sql`insert into users ${sql(users, 'name', 'age')} returning id`; // fine
await sql<{ id: number }[]>`insert into users ${sql(users, 'name', 'age')} returning id`; // Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.

something I'm doing wrong?

noslouch avatar Oct 10 '24 14:10 noslouch

The following works without a type error as well.

await sql<{ id: number }[]>`insert into users ${sql(users)} returning id`; 

My guess is that it's another case of the inferred typing problem that @porsager is trying to solve in v4.

You can get around this for the time being with an explicit casting.

await sql<{ id: number }[]>`insert into users ${sql(users, 'name', 'age') as any} returning id`; 

Louis-Tian avatar Oct 12 '24 01:10 Louis-Tian

thanks. good suggestion. of course now eslint is complaining about using any, but I can live with that. I'd like to leave this open for posterity, in the hopes a fix lands in 4.

noslouch avatar Oct 23 '24 19:10 noslouch

FWIW never seems to work for me without complaint from ts or eslint.

noslouch avatar Oct 23 '24 19:10 noslouch