What's the best way to use Pick with Selectable/Insertable/Updatable?

I am newer to typescript so fully possible missing something obvious here. But if I try to Pick from a table schema wrapped in Selectable/Insertable/Updateable it doesn't seem to infer the actual type of the column. Example (first screenshot): x has no typescript issue here (just warns its unused in this fake code). "test" type shows resolving as:
type test = {
email: Selectable<UserTable>;
}
type test = {
email: Selectable<UserTable>;
}
Versus if I actually reference UserTable (second screenshot) I get TS Error:
Type '() => void' is not assignable to type 'string'.ts(2322)
Type '() => void' is not assignable to type 'string'.ts(2322)
But there is warning in the docs: "// You should not use the table schema interfaces directly." so I've tried to avoid refering UserTable directly.
No description
No description
Solution:
Actually I think this is environmental - I found there is a kysely sandbox and I couldn't repro there - it works perfect there in how I showed/you showed in the "better yet". I am going to close this and thanks again @koskimas
Jump to solution
3 Replies
koskimas
koskimas5mo ago
Your first example:
type test = {
email: Selectable<UserTable>;
}
type test = {
email: Selectable<UserTable>;
}
creates a type like this:
type test = {
email: {
id: number
first_name: string
last_name: string
email: string
...
}
}
type test = {
email: {
id: number
first_name: string
last_name: string
email: string
...
}
}
If you want the type of an individual column, you need to do something like this:
type test = {
email: Selectable<UserTable>["email"];
}
type test = {
email: Selectable<UserTable>["email"];
}
Or better yet:
type User = Selectable<UserTable>
type test = Pick<User, "email">
type User = Selectable<UserTable>
type test = Pick<User, "email">
Your second example attempts to assign a function to a variable that has the email column's type. I have no idea what you wanted it to do, but you can't assign a function to what I assume to be a string.
WhyDoThis
WhyDoThisOP5mo ago
Thanks for checking this out. Yup I definitely think you misunderstood. To recap, you can see in first screenshot I did what your "better yet" shows (the User type for it is in fact Selectable<UserTable> as can be seen in second screenshot and referenced in the OP "But if I try to Pick from a table schema wrapped in Selectable/Insertable/Updateable..."). When you said "my first example" you pasted not what I attempted but what I wrote in the OP that the pick type in my first example/your "better yet" shows in VS code to resolve to (when I hover over it). Of course that type would make no sense. I used the function assignment to clearly show it doesn't error on something it should. Second screenshot example was just to demonstrate it does in fact error if I reference directly from UserTable. (Upon review though I did see in my OP one small mistake which is I accidently pasted the intentionally wrong function assignment line again instead of the error for the second example - I've corrected that bit now.)
Solution
WhyDoThis
WhyDoThis5mo ago
Actually I think this is environmental - I found there is a kysely sandbox and I couldn't repro there - it works perfect there in how I showed/you showed in the "better yet". I am going to close this and thanks again @koskimas
Want results from more Discord servers?
Add your server