Skip to content

Commit e8dfde6

Browse files
authored
Merge pull request #12284 from Automattic/vkarpov15/gh-10349-202208
Speed up TypeScript perf
2 parents e43eba9 + b1c66c2 commit e8dfde6

File tree

3 files changed

+37
-51
lines changed

3 files changed

+37
-51
lines changed

types/index.d.ts

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ declare module 'mongoose' {
6262
export function get<K extends keyof MongooseOptions>(key: K): MongooseOptions[K];
6363

6464
/* ! ignore */
65-
export type CompileModelOptions = { overwriteModels?: boolean, connection?: Connection };
65+
export type CompileModelOptions = {
66+
overwriteModels?: boolean,
67+
connection?: Connection
68+
};
6669

6770
export function model<TSchema extends Schema = any>(
6871
name: string,
@@ -113,10 +116,6 @@ declare module 'mongoose' {
113116
? IfAny<U, T & { _id: Types.ObjectId }, T & Required<{ _id: U }>>
114117
: T & { _id: Types.ObjectId };
115118

116-
export type RequireOnlyTypedId<T> = T extends { _id?: infer U; }
117-
? Required<{ _id: U }>
118-
: { _id: Types.ObjectId };
119-
120119
export type HydratedDocument<DocType, TMethodsAndOverrides = {}, TVirtuals = {}> = DocType extends Document ? Require_id<DocType> : (Document<unknown, any, DocType> & Require_id<DocType> & TVirtuals & TMethodsAndOverrides);
121120

122121
export interface TagSet {
@@ -196,7 +195,7 @@ declare module 'mongoose' {
196195
/** Returns a copy of this schema */
197196
clone<T = this>(): T;
198197

199-
discriminator<T extends Schema = Schema>(name: string, schema: T): DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, T>;
198+
discriminator<T = Schema>(name: string, schema: T): DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, T>;
200199

201200
/** Returns a new schema that has the picked `paths` from this schema. */
202201
pick<T = this>(paths: string[], options?: SchemaOptions): T;
@@ -257,17 +256,17 @@ declare module 'mongoose' {
257256
/** Defines a post hook for the model. */
258257
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, T>): this;
259258
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
260-
post<T extends Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | string | RegExp, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
261-
post<T extends Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | string | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
259+
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
260+
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
262261
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>): this;
263262
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>): this;
264263
post<T = M>(method: 'insertMany' | RegExp, fn: PostMiddlewareFunction<T, T>): this;
265264
post<T = M>(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
266265

267266
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
268267
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;
269-
post<T extends Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | string | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
270-
post<T extends Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | string | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;
268+
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
269+
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;
271270
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: ErrorHandlingMiddlewareFunction<T, Array<any>>): this;
272271
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T, Array<any>>): this;
273272
post<T = M>(method: 'insertMany' | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
@@ -278,9 +277,8 @@ declare module 'mongoose' {
278277
pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: 'save', options: SchemaPreOptions, fn: PreSaveMiddlewareFunction<T>): this;
279278
pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this;
280279
pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
281-
pre<T extends Query<any, any>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
282-
pre<T extends Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | string | RegExp, fn: PreMiddlewareFunction<T>): this;
283-
pre<T extends Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | string | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
280+
pre<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this;
281+
pre<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
284282
pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PreMiddlewareFunction<T>): this;
285283
pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
286284
pre<T = M>(method: 'insertMany' | RegExp, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array<any>) => void | Promise<void>): this;
@@ -452,10 +450,6 @@ declare module 'mongoose' {
452450

453451
export type SortOrder = -1 | 1 | 'asc' | 'ascending' | 'desc' | 'descending';
454452

455-
type Mutable<T> = {
456-
-readonly [K in keyof T]: T[K];
457-
};
458-
459453
type _UpdateQuery<TSchema> = {
460454
/** @see https://docs.mongodb.com/manual/reference/operator/update-field/ */
461455
$currentDate?: AnyKeys<TSchema> & AnyObject;
@@ -469,7 +463,7 @@ declare module 'mongoose' {
469463
$unset?: AnyKeys<TSchema> & AnyObject;
470464

471465
/** @see https://docs.mongodb.com/manual/reference/operator/update-array/ */
472-
$addToSet?: Mutable<mongodb.SetFields<TSchema>>;
466+
$addToSet?: AnyKeys<TSchema> & AnyObject;
473467
$pop?: AnyKeys<TSchema> & AnyObject;
474468
$pull?: AnyKeys<TSchema> & AnyObject;
475469
$push?: AnyKeys<TSchema> & AnyObject;
@@ -487,21 +481,14 @@ declare module 'mongoose' {
487481
{ $replaceRoot: any } |
488482
{ $replaceWith: any };
489483

490-
export type __UpdateDefProperty<T> =
491-
[Extract<T, mongodb.ObjectId>] extends [never] ? T :
492-
T | string;
493-
export type _UpdateQueryDef<T> = {
494-
[K in keyof T]?: __UpdateDefProperty<T[K]>;
495-
};
496-
497484
/**
498485
* Update query command to perform on the document
499486
* @example
500487
* ```js
501488
* { age: 30 }
502489
* ```
503490
*/
504-
export type UpdateQuery<T> = _UpdateQuery<_UpdateQueryDef<T>> & AnyObject;
491+
export type UpdateQuery<T> = _UpdateQuery<T> & AnyObject;
505492

506493
export type DocumentDefinition<T> = {
507494
[K in keyof Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>>]:
@@ -527,7 +514,6 @@ declare module 'mongoose' {
527514
T extends Types.Subdocument ? Omit<LeanDocument<T>, '$isSingleNested' | 'ownerDocument' | 'parent'> :
528515
LeanDocument<T>; // Documents and everything else
529516

530-
531517
export type LeanArray<T extends unknown[]> = T extends unknown[][] ? LeanArray<T[number]>[] : LeanType<T[number]>[];
532518

533519
export type _LeanDocument<T> = {

types/models.d.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -260,21 +260,21 @@ declare module 'mongoose' {
260260
init(callback?: CallbackWithoutResult): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>;
261261

262262
/** Inserts one or more new documents as a single `insertMany` call to the MongoDB server. */
263-
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { lean: true; }, callback: Callback<Array<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>>>): void;
263+
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { lean: true; }, callback: Callback<Array<MergeType<MergeType<T, DocContents>, Require_id<T>>>>): void;
264264
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { rawResult: true; }, callback: Callback<mongodb.InsertManyResult<T>>): void;
265-
insertMany<DocContents = T>(docs: Array<DocContents | T>, callback: Callback<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>): void;
266-
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean: true; }, callback: Callback<Array<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>>>): void;
265+
insertMany<DocContents = T>(docs: Array<DocContents | T>, callback: Callback<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>): void;
266+
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean: true; }, callback: Callback<Array<MergeType<MergeType<T, DocContents>, Require_id<T>>>>): void;
267267
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { rawResult: true; }, callback: Callback<mongodb.InsertManyResult<T>>): void;
268-
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean?: false | undefined }, callback: Callback<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>): void;
269-
insertMany<DocContents = T>(doc: DocContents, callback: Callback<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>): void;
268+
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean?: false | undefined }, callback: Callback<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>): void;
269+
insertMany<DocContents = T>(doc: DocContents, callback: Callback<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>): void;
270270

271-
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { lean: true; }): Promise<Array<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>>>;
271+
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { lean: true; }): Promise<Array<MergeType<MergeType<T, DocContents>, Require_id<T>>>>;
272272
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { rawResult: true; }): Promise<mongodb.InsertManyResult<T>>;
273-
insertMany<DocContents = T>(docs: Array<DocContents | T>): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>;
274-
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean: true; }): Promise<Array<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>>>;
273+
insertMany<DocContents = T>(docs: Array<DocContents | T>): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>;
274+
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean: true; }): Promise<Array<MergeType<MergeType<T, DocContents>, Require_id<T>>>>;
275275
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { rawResult: true; }): Promise<mongodb.InsertManyResult<T>>;
276-
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>;
277-
insertMany<DocContents = T>(doc: DocContents): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>;
276+
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>;
277+
insertMany<DocContents = T>(doc: DocContents): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>;
278278

279279
/** The name of the model */
280280
modelName: string;

types/query.d.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ declare module 'mongoose' {
271271
distinct<ReturnType = any>(field: string, filter?: FilterQuery<DocType>, callback?: Callback<number>): QueryWithHelpers<Array<ReturnType>, DocType, THelpers, RawDocType>;
272272

273273
/** Specifies a `$elemMatch` query condition. When called with one argument, the most recent path passed to `where()` is used. */
274-
elemMatch<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$elemMatch']): this;
274+
elemMatch<K = string>(path: K, val: any): this;
275275
elemMatch(val: Function | any): this;
276276

277277
/**
@@ -288,7 +288,7 @@ declare module 'mongoose' {
288288
estimatedDocumentCount(options?: QueryOptions<DocType>, callback?: Callback<number>): QueryWithHelpers<number, DocType, THelpers, RawDocType>;
289289

290290
/** Specifies a `$exists` query condition. When called with one argument, the most recent path passed to `where()` is used. */
291-
exists<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$exists']): this;
291+
exists<K = string>(path: K, val: boolean): this;
292292
exists(val: boolean): this;
293293

294294
/**
@@ -403,18 +403,18 @@ declare module 'mongoose' {
403403
getUpdate(): UpdateQuery<DocType> | UpdateWithAggregationPipeline | null;
404404

405405
/** Specifies a `$gt` query condition. When called with one argument, the most recent path passed to `where()` is used. */
406-
gt<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$gt']): this;
406+
gt<K = string>(path: K, val: any): this;
407407
gt(val: number): this;
408408

409409
/** Specifies a `$gte` query condition. When called with one argument, the most recent path passed to `where()` is used. */
410-
gte<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$gte']): this;
410+
gte<K = string>(path: K, val: any): this;
411411
gte(val: number): this;
412412

413413
/** Sets query hints. */
414414
hint(val: any): this;
415415

416416
/** Specifies an `$in` query condition. When called with one argument, the most recent path passed to `where()` is used. */
417-
in<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$in']): this;
417+
in<K = string>(path: K, val: any[]): this;
418418
in(val: Array<any>): this;
419419

420420
/** Declares an intersects query for `geometry()`. */
@@ -430,11 +430,11 @@ declare module 'mongoose' {
430430
limit(val: number): this;
431431

432432
/** Specifies a `$lt` query condition. When called with one argument, the most recent path passed to `where()` is used. */
433-
lt<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$lt']): this;
433+
lt<K = string>(path: K, val: any): this;
434434
lt(val: number): this;
435435

436436
/** Specifies a `$lte` query condition. When called with one argument, the most recent path passed to `where()` is used. */
437-
lte<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$lte']): this;
437+
lte<K = string>(path: K, val: any): this;
438438
lte(val: number): this;
439439

440440
/**
@@ -461,7 +461,7 @@ declare module 'mongoose' {
461461
merge(source: Query<any, any> | FilterQuery<DocType>): this;
462462

463463
/** Specifies a `$mod` condition, filters documents for documents whose `path` property is a number that is equal to `remainder` modulo `divisor`. */
464-
mod<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$mod']): this;
464+
mod<K = string>(path: K, val: number): this;
465465
mod(val: Array<number>): this;
466466

467467
/** The model this query was created from */
@@ -474,15 +474,15 @@ declare module 'mongoose' {
474474
mongooseOptions(val?: MongooseQueryOptions): MongooseQueryOptions;
475475

476476
/** Specifies a `$ne` query condition. When called with one argument, the most recent path passed to `where()` is used. */
477-
ne<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$ne']): this;
477+
ne<K = string>(path: K, val: any): this;
478478
ne(val: any): this;
479479

480480
/** Specifies a `$near` or `$nearSphere` condition */
481-
near<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$near']): this;
481+
near<K = string>(path: K, val: any): this;
482482
near(val: any): this;
483483

484484
/** Specifies an `$nin` query condition. When called with one argument, the most recent path passed to `where()` is used. */
485-
nin<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$nin']): this;
485+
nin<K = string>(path: K, val: any[]): this;
486486
nin(val: Array<any>): this;
487487

488488
/** Specifies arguments for an `$nor` condition. */
@@ -518,7 +518,7 @@ declare module 'mongoose' {
518518
readConcern(level: string): this;
519519

520520
/** Specifies a `$regex` query condition. When called with one argument, the most recent path passed to `where()` is used. */
521-
regex<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$regex']): this;
521+
regex<K = string>(path: K, val: RegExp): this;
522522
regex(val: string | RegExp): this;
523523

524524
/**
@@ -570,7 +570,7 @@ declare module 'mongoose' {
570570
setUpdate(update: UpdateQuery<DocType> | UpdateWithAggregationPipeline): void;
571571

572572
/** Specifies an `$size` query condition. When called with one argument, the most recent path passed to `where()` is used. */
573-
size<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$size']): this;
573+
size<K = string>(path: K, val: number): this;
574574
size(val: number): this;
575575

576576
/** Specifies the number of documents to skip. */
@@ -599,7 +599,7 @@ declare module 'mongoose' {
599599
then: Promise<ResultType>['then'];
600600

601601
/** Converts this query to a customized, reusable query constructor with all arguments and options retained. */
602-
toConstructor(): new (...args: any[]) => QueryWithHelpers<ResultType, DocType, THelpers, RawDocType>;
602+
toConstructor(): typeof this;
603603

604604
/** Declare and/or execute this query as an update() operation. */
605605
update(filter?: FilterQuery<DocType>, update?: UpdateQuery<DocType> | UpdateWithAggregationPipeline, options?: QueryOptions<DocType> | null, callback?: Callback<UpdateWriteOpResult>): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType>;

0 commit comments

Comments
 (0)