-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
Importing type from generated types #63
Comments
Interesting problem! I can see the use case. One possible solution would be to add a helper type to export type Model<T extends Record<PropertyKey, unknown>> = {
[K in keyof T]: T[K] extends ColumnType<infer S, any, any> ? S : T[K];
};
export type ExampleModel = Model<Example>;
// {
// id: number;
// created_at: Date;
// updated_at: Date;
// text: string;
// }; |
There's a From the example: import type { Selectable } from 'kysely'
import type { Examples } from "@db/schema";
type ExampleProps = {
example: Selectable<Examples>;
}; To convert the whole DB: import { Selectable } from 'kysely'
import type { DB } from './path-to-kyseley.gen'
type DBSelect = {
[K in keyof DB]: Selectable<DB[K]>
} |
Interesting! Is this a good enough solution for your use case @schwartzmj? |
I'd really appreciate kysely-codegen generating something like export type Examples = { ... }
export type Example = Selectable<Examples> This is how I did it when I wrote the types manually -- we won't have to put Selectable<...> everywhere, which gets cumbersome if you need other helper types quickly (imagine |
I wrote this abomination:
This allowed me to write functions like the below, with a good degree of type safety
The only thing I'm figuring out is how to get the third argument (which is the selectors I want to use), to properly restrict the properties on the return object. Not quite there yet. edit: I tried more, but hitting a brick wall. going to stick with the code above for now
edit... improved... sticking with this one instead:
|
It would be awesome + in-line with the intended use of Kysely if kysely-codegen exported the selectable insertable and updateable types. Something like this: export interface UserTable {
id: Generated<string>
...
}
export type User = Selectable<UserTable>
export type NewUser = Insertable<UserTable>
export type UserUpdate = Updateable<UserTable> This is how we instruct people to structure their types in the docs. |
+1 |
Let's say I have a table called
Examples
and the generated types look like this:If I'm using something like React or Svelte, and I want to type a prop e.g.:
I'll get errors like the following for the
Generated<number>
on theid
column (in this example, trying to set thevalue
attribute on aninput
element):Any way around this? Would it be best to create a file of different queries and infer the type of each result and export those:
This seems a bit odd, however if I have different queries e.g. one where I
selectAll
vs selecting certain fields, it seems great to have those typed separately and then I can just import the query from the file as well.Upvote & Fund
The text was updated successfully, but these errors were encountered: