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

[WIP] fix: resolve and unwrap AsyncHasMany for store.createRecord() #9402

Closed

Conversation

seanCodes
Copy link
Contributor

This is a WIP. The code works as expected but a cyclical dependency was introduced that needs to be resolved.

Description

In #9396 the prop types expected by store.createRecord() were updated to resolve AsyncBelongsTo to the underlying model.

AsyncHasMany wasn't handled though, so this PR addresses that. To do so the CreateRecordProperties type util was updated to look for RelatedCollections (which are the result of Awaited-ing the AsyncHasMany type) and then the model used in the collection is turned into a plain array of that model type. This allows the following:

const parentModel = store.createRecord<ParentModel>('parent', { children: [existingChildModel] })

Previously this would error because [existingChildModel] in the model props wasn't of type RelatedCollection<ChildModel>.

Notes for the release

N/A

// For `AsyncHasMany`: apply `Awaited` and check if the type is a `RelatedCollection`. If so,
// unwrap it and convert it to a plain array of the model type. For everything else (including
// `AsyncBelongsTo`): apply `Awaited`.
[K in keyof T]: NonNullable<Awaited<T[K]>> extends RelatedCollection<infer M> ? Array<M> : Awaited<T[K]>;
Copy link
Contributor

Choose a reason for hiding this comment

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

it should work here to check if it extends Array<infer M>

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

Successfully merging this pull request may close these issues.

None yet

2 participants