Skip to content

Commit 48988be

Browse files
committed
types: some performance optimizations and crank up max instantiations
1 parent 4bb2f77 commit 48988be

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

scripts/tsc-diagnostics-check.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const fs = require('fs');
44

55
const stdin = fs.readFileSync(0).toString('utf8');
6-
const maxInstantiations = isNaN(process.argv[2]) ? 300000 : parseInt(process.argv[2], 10);
6+
const maxInstantiations = isNaN(process.argv[2]) ? 350000 : parseInt(process.argv[2], 10);
77

88
console.log(stdin);
99

types/inferrawdoctype.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ declare module 'mongoose' {
9797
ResolveRawPathType<Options['of'] extends ReadonlyArray<infer Item> ? Item : never>
9898
: PathValueType extends ArrayConstructor ? any[]
9999
: PathValueType extends typeof Schema.Types.Mixed ? any
100-
: IfEquals<PathValueType, ObjectConstructor> extends true ? any
100+
: PathValueType extends ObjectConstructor ? any
101101
: IfEquals<PathValueType, {}> extends true ? any
102102
: PathValueType extends typeof SchemaType ? PathValueType['prototype']
103103
: PathValueType extends Record<string, any> ? InferRawDocType<PathValueType>

types/inferschematype.d.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ type IsPathRequired<P, TypeKey extends string = DefaultTypeKey> =
150150
: P extends Record<TypeKey, ArrayConstructor | any[]> ? true
151151
: false;
152152

153+
// Internal type used to efficiently check for never or any types
154+
// can be efficiently checked like:
155+
// `[T] extends [neverOrAny] ? T : ...`
156+
// to avoid edge cases
157+
type neverOrAny = ' ~neverOrAny~';
158+
153159
/**
154160
* @summary A Utility to obtain schema's required path keys.
155161
* @param {T} T A generic refers to document definition.
@@ -228,6 +234,7 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> =
228234

229235
type IsSchemaTypeFromBuiltinClass<T> =
230236
T extends typeof String ? true
237+
: unknown extends Buffer ? false
231238
: T extends typeof Number ? true
232239
: T extends typeof Boolean ? true
233240
: T extends typeof Buffer ? true
@@ -244,7 +251,6 @@ type IsSchemaTypeFromBuiltinClass<T> =
244251
: T extends Types.Decimal128 ? true
245252
: T extends NativeDate ? true
246253
: T extends typeof Schema.Types.Mixed ? true
247-
: unknown extends Buffer ? false
248254
: T extends Buffer ? true
249255
: false;
250256

@@ -260,12 +266,10 @@ type ResolvePathType<
260266
Options extends SchemaTypeOptions<PathValueType> = {},
261267
TypeKey extends string = DefaultSchemaOptions['typeKey'],
262268
TypeHint = never
263-
> = IfEquals<
264-
TypeHint,
265-
never,
266-
PathValueType extends Schema ? InferSchemaType<PathValueType>
269+
> = [TypeHint] extends [never]
270+
? PathValueType extends Schema ? InferSchemaType<PathValueType>
267271
: PathValueType extends AnyArray<infer Item> ?
268-
IfEquals<Item, never> extends true
272+
[Item] extends [never]
269273
? any[]
270274
: Item extends Schema ?
271275
// If Item is a schema, infer its type.
@@ -304,7 +308,7 @@ type ResolvePathType<
304308
: never
305309
: PathValueType extends ArrayConstructor ? any[]
306310
: PathValueType extends typeof Schema.Types.Mixed ? any
307-
: IfEquals<PathValueType, ObjectConstructor> extends true ? any
311+
: PathValueType extends ObjectConstructor ? any
308312
: IfEquals<PathValueType, {}> extends true ? any
309313
: PathValueType extends typeof SchemaType ? PathValueType['prototype']
310314
: PathValueType extends Record<string, any> ?
@@ -315,6 +319,5 @@ type ResolvePathType<
315319
typeKey: TypeKey;
316320
}
317321
>
318-
: unknown,
319-
TypeHint
320-
>;
322+
: unknown
323+
: TypeHint;

types/virtuals.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ declare module 'mongoose' {
88
type TVirtualPathFN<DocType = {}, PathType = unknown, TInstanceMethods = {}, TReturn = unknown> =
99
<T = HydratedDocument<DocType, TInstanceMethods>>(this: Document<any, any, DocType> & DocType, value: PathType, virtual: VirtualType<T>, doc: Document<any, any, DocType> & DocType) => TReturn;
1010

11-
type SchemaOptionsVirtualsPropertyType<DocType = any, VirtualPaths = Record<any, unknown>, TInstanceMethods = {}> = {
12-
[K in keyof VirtualPaths]: VirtualPathFunctions<IsItRecordAndNotAny<DocType> extends true ? DocType : any, VirtualPaths[K], TInstanceMethods>
13-
};
11+
type SchemaOptionsVirtualsPropertyType<DocType = any, VirtualPaths = Record<any, unknown>, TInstanceMethods = {}> = {
12+
[K in keyof VirtualPaths]: VirtualPathFunctions<IsItRecordAndNotAny<DocType> extends true ? DocType : any, VirtualPaths[K], TInstanceMethods>
13+
};
1414
}

0 commit comments

Comments
 (0)