Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions test/types/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1056,3 +1056,31 @@ async function gh16526() {
const insertManyResult = await Tank.insertMany([{ name: 'test' }], { lean: true, rawResult: true });
expectType<number>(insertManyResult.insertedCount);
}

async function gh15693() {
interface IUser {
name: string;
}

interface UserMethods {
printName(this: IUser): void;
getName(): string;
}

const schema = new Schema<IUser, Model<IUser>, UserMethods>({ name: { type: String, required: true } });
schema.method('printName', function printName(this: IUser) {
expectError(this.isModified('name'));
expectError(this.doesNotExist());
expectType<string>(this.name);
console.log(this.name);
});
schema.method('getName', function getName() {
expectType<boolean>(this.isModified('name'));
return this.name;
});
const User = model('user', schema);

const leanInst = await User.findOne({}).lean().orFail();
User.schema.methods.printName.apply(leanInst);

}
2 changes: 1 addition & 1 deletion types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ declare module 'mongoose' {
recompileSchema(): void;

/** Schema the model uses. */
schema: Schema<TRawDocType>;
schema: TSchema;

/** Creates a `updateMany` query: updates all documents that match `filter` with `update`. */
updateMany<ResultDoc = THydratedDocumentType>(
Expand Down
4 changes: 2 additions & 2 deletions types/utility.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ declare module 'mongoose' {
type IfAny<IFTYPE, THENTYPE, ELSETYPE = IFTYPE> = 0 extends 1 & IFTYPE
? THENTYPE
: ELSETYPE;
type IfUnknown<IFTYPE, THENTYPE> = unknown extends IFTYPE ? THENTYPE : IFTYPE;
type IsUnknown<T> = unknown extends T ? true : false;

type WithLevel1NestedPaths<T, K extends keyof T = keyof T> = {
[P in K | NestedPaths<Required<T>, K>]: P extends K
Expand Down Expand Up @@ -135,7 +135,7 @@ declare module 'mongoose' {
*/
type AddThisParameter<T, D> = {
[K in keyof T]: T[K] extends (...args: infer A) => infer R
? ThisParameter<T[K], unknown> extends unknown
? IsUnknown<ThisParameter<T[K], unknown>> extends true
? (this: D, ...args: A) => R
: T[K]
: T[K];
Expand Down
Loading