-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Conversation
There was a problem hiding this 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'.
@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. |
i am sorry, i missed testing it against the master branch too, i forgot to add updated scriptasync 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 |
Fix #14441
Summary
When you pass a generic to
populate()
, likepopulate<{ child: Child }>('child')
, to override the result type, callingtoObject()
loses the{ child: Child }
type override by default.Future work: if passing
depopulate: true
totoObject()
, that removes the populated paths.Examples