Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Istanful
Copy link

@Istanful Istanful commented Feb 9, 2022

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

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.
@IanVS
Copy link
Collaborator

IanVS commented Feb 16, 2022

Thanks for this! I tested it out in my own project by copying the type file into my node_modules, and while it does allow me to remove my // @ts-expect-error comments (\o/), I start to get some other errors unfortunately.

  • I now get a type error on my overall factories definition:
    'factories' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

  • I'm no longer able to access attributes on the factory as I used to.
    image
    vs previously:
    image

  • I seem to have lost the types when I use schema.all():
    image
    vs previously:
    image

I'm not sure what needs to be done to address these, but if you're able to take another crack at it, I'd love to give it another review. Just send me a ping to be sure I see it. Thanks again for your efforts!

Copy link

@Nol-go Nol-go left a 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

Comment on lines +183 to +185
type WithFactoryMethods<T> =
| { afterCreate?: (model: ModelInstance<any>, server: Server) => void }
| { [K in keyof Omit<T, "afterCreate">]: T[K] | ((n: number) => T[K]) };
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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])
};

Comment on lines +189 to +191
[K in keyof Omit<T, "afterCreate">]: T[K] extends (n: number) => infer V
? V
: T[K];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[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];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Typescript] Unable to define afterCreate in factory (type error)
3 participants