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(index.d.ts): fix Document#populate() type #10651

Merged
merged 2 commits into from
Sep 3, 2021
Merged

fix(index.d.ts): fix Document#populate() type #10651

merged 2 commits into from
Sep 3, 2021

Conversation

thiagokisaki
Copy link
Contributor

Summary
According to mongoose docs, Document#populate() receives, as the first argument, the path to populate or an options object or an array of paths and/or options objects.
However, in the following code, there is a TypeScript error:

import { Schema, model } from 'mongoose';

const personSchema = new Schema({
  name: String,
  stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

const storySchema = new Schema({
  title: String,
  author: { type: Schema.Types.ObjectId, ref: 'Person' },
  fans: [{ type: Schema.Types.ObjectId, ref: 'Person' }]
});

const Person = model('Person', personSchema);
const Story = model('Story', storySchema);

(async () => {
  const story = await Story.findOne().orFail(new Error('Story not found'));
  
  await story.populate(['author', { path: 'fans' }]); // This line is causing the error
})();
No overload matches this call.
  Overload 1 of 6, '(path: string | string[]): Promise<Document<any, any, unknown>>', gave the following error.
    Type '{ path: string; }' is not assignable to type 'string'.
  Overload 2 of 6, '(opts: PopulateOptions | PopulateOptions[]): Promise<Document<any, any, unknown>>', gave the following error.
    Type 'string' is not assignable to type 'PopulateOptions'.ts(2769)

This PR solves the problem, allowing us to provide an array mixing paths and option objects.

Examples
Now, all of these lines are valid:

  await story.populate('author');
  await story.populate({ path: 'fans' });
  await story.populate(['author']);
  await story.populate([{ path: 'fans' }]);
  await story.populate(['author', { path: 'fans' }]);

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.

Looks great, would appreciate some tests proving that this works.

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Great work, thanks 👍

@vkarpov15 vkarpov15 added this to the 6.0.5 milestone Sep 3, 2021
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Sep 3, 2021
@vkarpov15 vkarpov15 merged commit f67e31f into Automattic:master Sep 3, 2021
@thiagokisaki thiagokisaki deleted the patch-9 branch September 4, 2021 12:32
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

3 participants