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

Support defining the type when extending a factory with params() #75

Closed
wants to merge 1 commit into from

Conversation

lo1tuma
Copy link

@lo1tuma lo1tuma commented Sep 9, 2021

Fixes: #71

Copy link
Contributor

@stevehanson stevehanson left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution. There are a couple issues here that circumvent the type-checker which would prevent me from accepting this as is. We will need a solution that does not do a type coercion (eg. as unknown as TOverride) so that we can guarantee that types are working correctly.

const factory = this.clone();
factory._params = merge({}, this._params, params, mergeCustomizer);
return factory;
return factory as unknown as Factory<TOverride, I>;
Copy link
Contributor

Choose a reason for hiding this comment

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

The issue, and I'm not sure yet how to solve it, is that even though TOverride extends T, we aren't guaranteeing that new params are passed in such that the built object is actually a TOverride.

Copy link
Author

Choose a reason for hiding this comment

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

You are absolutely right, params is Partial and might not contain all properties required for TOverride. 🤔 I also can’t think about any other approach to make it work with the params() method. Maybe with a dedicated extends() method that accepts another generator function.


it('adds parameters for a sub-type which are then also accepted in build()', () => {
const adminFactory = userFactory.params<AdminUser>({
admin: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

My concern is that if admin: true were not supplied, then the factory happily compiles but does not actually return an AdminUser.

@lo1tuma
Copy link
Author

lo1tuma commented Sep 19, 2021

Thanks for your feedback. I’m closing the PR as the idea doesn’t work as I thought.

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.

Is there a way to specify the type when extending factories?
2 participants