Are dynamic selects with type inference possible?
Title. I'm trying to add select statements conditionally. I'd like the function to return a properly typed query based on what I've passed. Is that possible?
This prevents errors but isn't really what I want.
1 Reply
After I worked on it a little bit I came up with this.
It seems to know now that irrigationClientId is a field on the subquery that gets returned from the function:
But what if I just want to pass "groupByIrrigationClientId: true"? Can I get type inference from the function somehow?
I guess the problem with adding
<T & SelectedFields<PgColumn, PgTable>>
to the select function is that you don't get type inference on the other fields. How do I fix that? lol
Removing the explicit type from the select function seems to have restored type inference on the other fields now.
The column in the if statement is also not being properly inferred but I'm guessing this has to do with my use of Object.values?
Is this too complex? lol
Or actually I guess the issue is that groupBy needs a column so I can just cast column to a PgColumn.
Settled on this.
Still wondering if it's possible to get type inference from passing a boolean to the function though.