types(query+populate): apply populate overrides to doc toObject() result - #14525
Conversation
hasezoey
left a comment
There was a problem hiding this comment.
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: truetotoObject(), that removes the populated paths.Examples