Help with typing for accepting optional select columns via function arguments #4569
Unanswered
DanielBaulig
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've been trying to wrap my head around this for over a day now. I searched, read documentation for Typescript and Drizzle up and down, tried using ChatGPT, experimented with various solutions. Nothing was able to give me a solution. I am trying to write a loadUser function that loads a user from the db. As an optional argument I want to provide the function a set of columns to include in the resulting row in addition to the default specified in the actual function. Here's what I currently have which appears to be working:
I can call this like follows and it appears to work:
As soon as I make the fields argument optional though, everything falls apart and the typing on the return user object is missing the properties specified on the fields object. I'm not sure why or how to prevent that. I've also tried a couple approaches of using conditional types and explicit return types, but I couldn't get the explicit return types to work at all. I tried using SelectResult<F & UserCoreColumns> and various conditional variations thereof, but the value actually returned from db.select never matched my attempts to explicitly define the return type. The types are so large and complex that I find it really hard to parse the error messages in a meaningful way. I am suspecting that the type mismatch when explicitly trying to define the return type might be due to how typescript merges object types, but I'm not sure.
I'd really love so help getting this to work since I'm at the end of my rope -- and nerves -- here.
Beta Was this translation helpful? Give feedback.
All reactions