Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing with inserts #958

Open
noslouch opened this issue Oct 10, 2024 · 3 comments
Open

Typing with inserts #958

noslouch opened this issue Oct 10, 2024 · 3 comments

Comments

@noslouch
Copy link

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?

@Louis-Tian
Copy link

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`; 

@noslouch
Copy link
Author

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@noslouch @Louis-Tian and others