-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Help Creating Type-Safe Models #282
Comments
Hi, @kjrocker. This is a valid concern. I'm working on the updated version of this library that covers improves type-safety as well. I can't promise to implement this into the existing version due to the lack of availability. But I do hope to get the new version out sometime soon (~this year). In the next version, I tended to give the type Models = {
user: {
id: Number
}
book: {
id: number
author: Relationship<'oneOf', 'user'>
}
}
const db = factory<Models>({
user: {
id: id(Number)
},
book: {
id: id(Number),
author: oneOf('user')
}
}) This is a bit tricky but I think I'm slowly getting it right internally. If you have some use cases to share please do, they can help a lot! |
Same problem here |
+++ |
I'm working on an application where all the BE types are compiled into Typescript for me. I would love to be able to enforce type-safety on the models in
factory
to some extent.I've got the following code
Obviously because of the getters I can't just do
user: { .... } as User
I also tried defining
type Factory<T> = { [P in keyof T]-?: () => T[P] };
With that, I can do
user: { .... } as Factory<User>
, which works for simple fields, but breaks for anything usingprimaryKey
,manyOf
,oneOf
, etc.Any thoughts? I'd really just like to make sure I'm not missing keys when I define new models.
The text was updated successfully, but these errors were encountered: