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

fix(document): allow calling $assertPopulated() with values to better support manual population #12341

Merged
merged 2 commits into from Aug 30, 2022

Conversation

vkarpov15
Copy link
Collaborator

Fix #12233

Summary

#12233 highlighted a gap in $assertPopulated(): there's no way to manually populate using =.

// TypeScript complains about incorrect type
newParent.nestedChild = new NestedChildModel({ name: 'NestedChild' })

newParent.$assertPopulated<{ nestedChild: NestedChildModel }>('nestedChild')
// Throws at runtime because `nestedChild` isn't populated yet
newParent.$assertPopulated<{ nestedChild: NestedChildModel }>('nestedChild')

newParent.nestedChild = new NestedChildModel({ name: 'NestedChild' })

This PR combines the two steps into 1 for TypeScript's benefit:

// Does `$assertPopulated()` and `$set()` in one operation
const doc = newParent.$assertPopulated<{nestedChild: NestedChild}>('nestedChild', {
  nestedChild: new NestedChildModel({ name: 'NestedChild' })
})

What do you think of this @ajwootto ?

Examples

Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

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

LGTM, thanks. 👍

lib/document.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

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

LGTM

@vkarpov15 vkarpov15 merged commit b60d0e6 into master Aug 30, 2022
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-12233 branch August 30, 2022 18:54
@hasezoey hasezoey added this to the 6.5.4 milestone Sep 1, 2022
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: Can't represent newly-constructed document as "populated"
3 participants