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

types(query+populate): apply populate overrides to doc toObject() result #14525

Merged
merged 3 commits into from
Apr 24, 2024

Conversation

vkarpov15
Copy link
Collaborator

Fix #14441

Summary

When you pass a generic to populate(), like populate<{ child: Child }>('child'), to override the result type, calling toObject() loses the { child: Child } type override by default.

Future work: if passing depopulate: true to toObject(), that removes the populated paths.

Examples

@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Apr 15, 2024
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.

This PR caused a error in typegoose, here some repro code & error:

code:

type BeAnObject = Record<string, any>;

interface SomeDoc {
  something: string;
  func(this: TestDoc): string;
}

interface PluginExtras {
  pfunc(): number;
}

type TestDoc = mongoose.Document<unknown, BeAnObject, SomeDoc> & PluginExtras;

type ModelType = mongoose.Model<SomeDoc, BeAnObject>;

const doc = await ({} as ModelType).findOne({}).populate('test').orFail().exec();

// error on "doc" here
doc.func();

let doc2 = await ({} as ModelType).create({});
// error on "doc" here
doc2 = await ({} as ModelType).findOne({}).populate('test').orFail().exec();

error:

The 'this' context of type 'Document<unknown, BeAnObject, Omit<SomeDoc, never>> & Omit<SomeDoc, never> & { _id: ObjectId; }' is not assignable to method's 'this' of type 'TestDoc'.
  Property 'pfunc' is missing in type 'Document<unknown, BeAnObject, Omit<SomeDoc, never>> & Omit<SomeDoc, never> & { _id: ObjectId; }' but required in type 'PluginExtras'.ts(2684)

Note: when modifying to type TestDoc = mongoose.Document<unknown, BeAnObject, SomeDoc & PluginExtras> it would fix the first error (doc.func()), but does not fix the second (doc2 = (await model.find.populate())).

original error:

Type 'Document<unknown, BeAnObject, Omit<Person, never>> & Omit<Person, never> & { _id: ObjectId; }' is not assignable to type 'Document<unknown, BeAnObject, Person> & Omit<Person & { _id: ObjectId; }, "typegooseName"> & IObjectWithTypegooseFunction'.
  Property 'typegooseName' is missing in type 'Document<unknown, BeAnObject, Omit<Person, never>> & Omit<Person, never> & { _id: ObjectId; }' but required in type 'IObjectWithTypegooseFunction'.

(line here)

@vkarpov15
Copy link
Collaborator Author

@hasezoey the script you provided seems to also fail to compile with same error against Mongoose master branch, so I'm not sure that error is due to this PR. Can you check if you get the same error on Mongoose master branch?

import mongoose from 'mongoose';

type BeAnObject = Record<string, any>;

interface SomeDoc {
  something: string;
  func(this: TestDoc): string;
}

interface PluginExtras {
  pfunc(): number;
}

type TestDoc = mongoose.Document<unknown, BeAnObject, SomeDoc> & PluginExtras;

type ModelType = mongoose.Model<SomeDoc, BeAnObject>;

async function foo() {
const doc = await ({} as ModelType).findOne({}).populate('test').orFail().exec();

// error on "doc" here
doc.func();

let doc2 = await ({} as ModelType).create({});
// error on "doc" here
doc2 = await ({} as ModelType).findOne({}).populate('test').orFail().exec();
}

I pushed a commit that may help resolve the issue you're seeing just in case the issue you're seeing happens to be independent of the repro script you posted. Try that out please.

@hasezoey
Copy link
Collaborator

hasezoey commented Apr 24, 2024

@hasezoey the script you provided seems to also fail to compile with same error against Mongoose master branch, so I'm not sure that error is due to this PR. Can you check if you get the same error on Mongoose master branch?

i am sorry, i missed testing it against the master branch too, i forgot to add PluginExtras to the mongoose.Model<_,_,HERE,_> type, here is the updated script:

updated script
async function main() {
type BeAnObject = Record<string, any>;

interface SomeDoc {
  something: string;
  func(this: TestDoc): string;
}

interface PluginExtras {
  pfunc(): number;
}

type TestDoc = mongoose.Document<unknown, BeAnObject, SomeDoc> & PluginExtras;

type ModelType = mongoose.Model<SomeDoc, BeAnObject, PluginExtras, BeAnObject>;

const doc = await ({} as ModelType).findOne({}).populate('test').orFail().exec();

// error on "doc" here
doc.func();

let doc2 = await ({} as ModelType).create({});
// error on "doc" here
doc2 = await ({} as ModelType).findOne({}).populate('test').orFail().exec();
}

and yes, the update fe1ed86 fixes the repro-script and the original error in typegoose

@vkarpov15 vkarpov15 added this to the 8.3.3 milestone Apr 24, 2024
@vkarpov15 vkarpov15 merged commit 5137eeb into master Apr 24, 2024
5 checks passed
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-14441 branch April 24, 2024 20:51
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.

(Typescript) toObject on populated documents loses the structure
2 participants