Skip to content

All methods required by TS when setting statics via Schema.static with object parameter #15780

@I-Enderlord-I

Description

@I-Enderlord-I

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.20.1

Node.js version

20.x

MongoDB server version

5.x

Typescript version (if applicable)

5.9.3

Description

Using the Schema.static method to set static model methods requires all methods to be passed. The current type definition is
(

static(obj: { [F in keyof TStaticMethods]: TStaticMethods[F] } & { [name: string]: (this: TModelType, ...args: any[]) => any }): this;

As seen there, the keys of TStaticMethods are all required.

This also causes problems with writing a generic Schema type.

Steps to Reproduce

Test:

interface IDoc {
}

interface DocStatics {
	m1(): void;
	m2(): void;
}

const schema = new Schema<IDoc, Model<IDoc>, {}, {}, {}, DocStatics>({});
schema.static({ m1() {} }); //TS Error. Property m2 missing

For the second observation consider

schema satisfies Schema<any, Model<any>, {}, {}, {}, {}>;

This might fail for other reasons as well, but part of the problem is that {} is missing the properties in DocStatics, making the static function unassignable to it's "generic" counterpart. Even when using an index signature e.g. { [name: string]: () => never } the check will still fail because index signature properties are considered optional. If the

Expected Behavior

No error.

The should allow defining the statics one by one as with Schema.method where the parameter is obj: Partial<TInstanceMethods>.
In this case the type thus should be

static(obj: { [F in keyof TStaticMethods]?: TStaticMethods[F] } & { [name: string]: (this: TModelType, ...args: any[]) => any }): this;

notice the added ? to make the keys optional.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions