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
Type afterCreate in factories #1019
base: master
Are you sure you want to change the base?
Conversation
When creating a factory when using typescript, the afterCreate hook could not be defined as it is not a key of the model. This commit adds typing for adding the afterCreate hook. Unfortunately the exact type of the model is not inferred in this commit. In order to make the factory fully infer the model, we would need to create a dependency between the factory type and the model type, which is something that seems to have been avoided purposefully. IMHO the any model instance works good enough for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try this. I did not test it but I guess it should work
type WithFactoryMethods<T> = | ||
| { afterCreate?: (model: ModelInstance<any>, server: Server) => void } | ||
| { [K in keyof Omit<T, "afterCreate">]: T[K] | ((n: number) => T[K]) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type WithFactoryMethods<T> = | |
| { afterCreate?: (model: ModelInstance<any>, server: Server) => void } | |
| { [K in keyof Omit<T, "afterCreate">]: T[K] | ((n: number) => T[K]) }; | |
type WithFactoryMethods<T> = { | |
afterCreate?: (model: ModelInstance<T>, server: Server) => void | |
[K in keyof T]: T[K] | ((n: number) => T[K]) | |
}; |
[K in keyof Omit<T, "afterCreate">]: T[K] extends (n: number) => infer V | ||
? V | ||
: T[K]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[K in keyof Omit<T, "afterCreate">]: T[K] extends (n: number) => infer V | |
? V | |
: T[K]; | |
[K in keyof T]: T[K] extends (n: number) => infer V ? V : T[K]; |
When creating a factory when using typescript, the afterCreate hook
could not be defined as it is not a key of the model. This commit adds
typing for adding the afterCreate hook.
Unfortunately the exact type of the model is not inferred in this
commit. In order to make the factory fully infer the model, we would
need to create a dependency between the factory type and the model type,
which is something that seems to have been avoided purposefully.
IMHO the any model instance works good enough for now.
Fixes #791