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

feat(types+document): add $assertPopulated() for working with manually populated paths in TypeScript #11843

Merged
merged 3 commits into from May 28, 2022

Conversation

vkarpov15
Copy link
Collaborator

Fix #11758

Summary

Right now, if you instantiate a document with manually populated paths, there's no way to indicate that a path is manually populated as opposed to just an ObjectId.

const parent = new ParentModel({
    nestedChild: new NestedChildModel({ name: 'test' }),
    name: 'Parent'
})

parent.nestedChild; // always ObjectId, even if manually populated

With this PR, you can at least do:

const parent = new ParentModel({
    nestedChild: new NestedChildModel({ name: 'test' }),
    name: 'Parent'
}).$assertPopulated<{ nestedChild: NestedChild }>('nestedChild')

doc.nestedChild.name; // string

which will make TypeScript work correctly, and also correctly throw a runtime error if 'nestedChild' isn't populated. What do you think @AbdelrahmanHafez @Uzlopak ?

Examples

@vkarpov15 vkarpov15 self-assigned this May 28, 2022
lib/document.js Outdated Show resolved Hide resolved
lib/document.js Outdated Show resolved Hide resolved
add comments on $assertPopulated docs
lib/document.js Outdated Show resolved Hide resolved
@AbdelrahmanHafez
Copy link
Collaborator

LGTM, thanks! 👍

One minor note though, it might be a good idea to give this label of typescript as well, because I first assumed it was a JS fix, not a mostly TS one.

@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label May 28, 2022
@vkarpov15 vkarpov15 changed the title feat(document): add $assertPopulated() for working with manually populated paths in TypeScript feat(types+document): add $assertPopulated() for working with manually populated paths in TypeScript May 28, 2022
@vkarpov15 vkarpov15 merged commit 2cff001 into 6.4 May 28, 2022
@vkarpov15 vkarpov15 deleted the vkarpov15/gh11758 branch May 28, 2022 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants