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

Fix #674 TypeScript issues with dynamic inserts #679

Merged
merged 1 commit into from
Oct 10, 2023

Conversation

alpharder
Copy link
Contributor

Fixes #674

The problem was caused by the nature of how rest operator ... is treated in TypeScript, implying readonly (Keys & keyof R)[] to represent individual function arguments.

[readonly (Keys & keyof R)[]] represents exactly one argument of type readonly (Keys & keyof R)[]

@porsager
Copy link
Owner

Mind approving this @Minigugus & @karlhorky ?

@karlhorky
Copy link
Contributor

karlhorky commented Sep 18, 2023

@alpharder can you show a TS Playground or CodeSandbox/StackBlitz with your Postgres.js code where this fails? Would be easier to review what it is you're doing here.

@alpharder
Copy link
Contributor Author

@karlhorky
Copy link
Contributor

karlhorky commented Sep 19, 2023

Thanks, from a code perspective that looks reasonable.

Do you have a documentation link for the sql(data, ["prop", "prop2"]) syntax? (and the sql(data, "prop", "prop2") syntax?). If so, that would be nice to link those two in the code comments as well.

My last question would be related to the ternary branches of the Rest generic type alias. I'm not super familiar with this type (maybe from @Minigugus ?) but would be maybe nice to write a line comment above each of these lines to describe what the difference is:

T extends readonly (object & infer R)[] ? (

T extends object ? (

@alpharder
Copy link
Contributor Author

Do you have a documentation link for the sql(data, ["prop", "prop2"]) syntax? (and the sql(data, "prop", "prop2") syntax?). If so, that would be nice to link those two in the code comments as well.

I guess the only documentation available is the README.md file.
There was no other documentation links in the types/index.d.ts so I haven't felt the necessity to introduce any innovations.

I'm not super familiar with this type (maybe from @Minigugus ?)

T extends readonly (object & infer R)[] means the first argument is a list of objects with inferred type R of list elements
T extends object means the first argument is an object

but would be maybe nice to write a line comment above each of these lines to describe what the difference is:

I'm afraid that for consistency we'd have to cover the whole Rest type definition with lots of commentaries, while this seems to be either out of the scope for the current fix and redundant since it's pretty much self-explanatory

@karlhorky
Copy link
Contributor

Ok, I'll leave these last decisions up to @porsager then, whether that's good enough like this - code looks ok, although a bit complex.

One last suggestion from my side: the suggested formatting of the union types in this PR is a bit unusual (see below)

  T extends readonly (object & infer R)[] ? (
    readonly (Keys & keyof R)[]   // sql(data, "prop", "prop2") syntax
    |
    [readonly (Keys & keyof R)[]] // sql(data, ["prop", "prop2"]) syntax
  ) :
  T extends readonly any[] ? readonly [] :
  T extends object ? (
    readonly (Keys & keyof T)[]   // sql(data, "prop", "prop2") syntax
    |
    [readonly (Keys & keyof T)[]] // sql(data, ["prop", "prop2"]) syntax
  ) :

Often unions are formatted without parentheses and by putting the pipe character at the start of each option in the union, eg. here's what Prettier does:

  : T extends readonly (object & infer R)[]
  ?
      | readonly (Keys & keyof R)[] // sql(data, "prop", "prop2") syntax
      | [readonly (Keys & keyof R)[]] // sql(data, ["prop", "prop2"]) syntax
  : T extends readonly any[]
  ? readonly []
  : T extends object
  ?
      | readonly (Keys & keyof T)[] // sql(data, "prop", "prop2") syntax
      | [readonly (Keys & keyof T)[]] // sql(data, ["prop", "prop2"]) syntax
  :

@alpharder
Copy link
Contributor Author

@porsager Could you please merge this and issue a new release?

@porsager
Copy link
Owner

@karlhorky @alpharder is there anything considered breaking changes in this PR? Asked another way - Am I gonna have a horde of angry Typescripters at my door if it goes in a minor release? 😂

@alpharder
Copy link
Contributor Author

Nope, they should be happy 😃

These changes are backwards-compatible

@porsager porsager merged commit 5f569d8 into porsager:master Oct 10, 2023
20 checks passed
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

Successfully merging this pull request may close these issues.

TypeScript issues with dynamic inserts
3 participants