Skip to content

Commit 40644db

Browse files
committed
fix(types): avoid making TInstanceMethods any by default leading to this = any in middleware
Fix #11435
1 parent 90e696c commit 40644db

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

test/types/document.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ function gh11435() {
159159
interface Item {
160160
name: string;
161161
}
162-
const ItemSchema = new Schema<Item, Model<Item>, {}>({ name: String });
162+
const ItemSchema = new Schema<Item>({ name: String });
163163

164164
ItemSchema.pre('validate', function preValidate() {
165165
expectType<Model<unknown>>(this.model('Item1'));

types/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ declare module 'mongoose' {
757757
export type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
758758
export type ErrorHandlingMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType, next: CallbackWithoutResultAndOptionalError) => void;
759759

760-
class Schema<DocType = any, M = Model<DocType, any, any, any>, TInstanceMethods = any, TQueryHelpers = any> extends events.EventEmitter {
760+
class Schema<DocType = any, M = Model<DocType, any, any, any>, TInstanceMethods = {}, TQueryHelpers = {}> extends events.EventEmitter {
761761
/**
762762
* Create a new schema
763763
*/
@@ -811,7 +811,7 @@ declare module 'mongoose' {
811811
method(obj: Partial<TInstanceMethods>): this;
812812

813813
/** Object of currently defined methods on this schema. */
814-
methods: { [F in keyof TInstanceMethods]: TInstanceMethods[F] };
814+
methods: { [F in keyof TInstanceMethods]: TInstanceMethods[F] } & AnyObject;
815815

816816
/** The original object passed to the schema constructor */
817817
obj: SchemaDefinition<SchemaDefinitionType<DocType>>;

0 commit comments

Comments
 (0)