Skip to content

Commit f6c7a6a

Browse files
committed
types: correctly pull schema definition from AutoInferredSchema
1 parent 7a0083e commit f6c7a6a

File tree

2 files changed

+60
-60
lines changed

2 files changed

+60
-60
lines changed

types/index.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ declare module 'mongoose' {
260260

261261
export class AutoInferredSchema<
262262
SchemaDef = unknown,
263-
RawDocType = any,
263+
RawDocType = InferRawDocType<SchemaDef>,
264264
TModelType = Model<RawDocType, any, any, any>,
265265
TInstanceMethods = {},
266266
TQueryHelpers = {},
@@ -276,9 +276,7 @@ declare module 'mongoose' {
276276
>,
277277
THydratedDocumentType = HydratedDocument<FlatRecord<DocType>, TVirtuals & TInstanceMethods>
278278
> extends Schema<RawDocType, TModelType, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods, TSchemaOptions, DocType, THydratedDocumentType> {
279-
constructor(definition?: SchemaDef, options?: SchemaOptions<FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
280-
281-
_schemaDefinition: SchemaDef;
279+
constructor(definition: SchemaDef, options?: SchemaOptions<FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
282280
}
283281

284282
export class Schema<

types/inferrawdoctype.d.ts

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ declare module 'mongoose' {
3636
TypeKey
3737
>;
3838

39-
type InferRawDocTypeFromSchema<TSchema extends Schema> = TSchema extends AutoInferredSchema
40-
? InferRawDocType<TSchema['_schemaDefinition']>
39+
type InferRawDocTypeFromSchema<TSchema extends Schema> = TSchema extends AutoInferredSchema<infer SchemaDef>
40+
? InferRawDocType<SchemaDef>
4141
: InferSchemaType<TSchema>;
4242

4343
/**
@@ -54,35 +54,22 @@ declare module 'mongoose' {
5454
* @returns Number, "Number" or "number" will be resolved to number type.
5555
*/
5656
type ResolveRawPathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends string = DefaultSchemaOptions['typeKey']> =
57-
PathValueType extends Schema ?
58-
InferRawDocTypeFromSchema<PathValueType> :
59-
PathValueType extends (infer Item)[] ?
60-
IfEquals<Item, never, any[], Item extends Schema ?
61-
// If Item is a schema, infer its type.
62-
Array<InferRawDocTypeFromSchema<Item>> :
63-
Item extends Record<TypeKey, any> ?
64-
Item[TypeKey] extends Function | String ?
65-
// If Item has a type key that's a string or a callable, it must be a scalar,
66-
// so we can directly obtain its path type.
67-
ObtainRawDocumentPathType<Item, TypeKey>[] :
68-
// If the type key isn't callable, then this is an array of objects, in which case
69-
// we need to call InferRawDocType to correctly infer its type.
70-
Array<InferRawDocType<Item>> :
71-
IsSchemaTypeFromBuiltinClass<Item> extends true ?
72-
ObtainRawDocumentPathType<Item, TypeKey>[] :
73-
IsItRecordAndNotAny<Item> extends true ?
74-
Item extends Record<string, never> ?
75-
ObtainRawDocumentPathType<Item, TypeKey>[] :
76-
Array<InferRawDocType<Item>> :
77-
ObtainRawDocumentPathType<Item, TypeKey>[]
78-
>:
79-
PathValueType extends ReadonlyArray<infer Item> ?
57+
PathValueType extends AutoInferredSchema<infer SchemaDef> ?
58+
InferRawDocType<SchemaDef> :
59+
PathValueType extends Schema ?
60+
InferSchemaType<PathValueType> :
61+
PathValueType extends (infer Item)[] ?
8062
IfEquals<Item, never, any[], Item extends Schema ?
63+
// If Item is a schema, infer its type.
8164
Array<InferRawDocTypeFromSchema<Item>> :
8265
Item extends Record<TypeKey, any> ?
8366
Item[TypeKey] extends Function | String ?
67+
// If Item has a type key that's a string or a callable, it must be a scalar,
68+
// so we can directly obtain its path type.
8469
ObtainRawDocumentPathType<Item, TypeKey>[] :
85-
InferRawDocType<Item>[]:
70+
// If the type key isn't callable, then this is an array of objects, in which case
71+
// we need to call InferRawDocType to correctly infer its type.
72+
Array<InferRawDocType<Item>> :
8673
IsSchemaTypeFromBuiltinClass<Item> extends true ?
8774
ObtainRawDocumentPathType<Item, TypeKey>[] :
8875
IsItRecordAndNotAny<Item> extends true ?
@@ -91,34 +78,49 @@ declare module 'mongoose' {
9178
Array<InferRawDocType<Item>> :
9279
ObtainRawDocumentPathType<Item, TypeKey>[]
9380
>:
94-
PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> :
95-
IfEquals<PathValueType, Schema.Types.String> extends true ? PathEnumOrString<Options['enum']> :
96-
IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']> :
97-
PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray<any> ? Options['enum'][number] : number :
98-
IfEquals<PathValueType, Schema.Types.Number> extends true ? number :
99-
PathValueType extends DateSchemaDefinition ? NativeDate :
100-
IfEquals<PathValueType, Schema.Types.Date> extends true ? NativeDate :
101-
PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
102-
PathValueType extends BooleanSchemaDefinition ? boolean :
103-
IfEquals<PathValueType, Schema.Types.Boolean> extends true ? boolean :
104-
PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId :
105-
IfEquals<PathValueType, Types.ObjectId> extends true ? Types.ObjectId :
106-
IfEquals<PathValueType, Schema.Types.ObjectId> extends true ? Types.ObjectId :
107-
PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 :
108-
IfEquals<PathValueType, Schema.Types.Decimal128> extends true ? Types.Decimal128 :
109-
IfEquals<PathValueType, Types.Decimal128> extends true ? Types.Decimal128 :
110-
IfEquals<PathValueType, Schema.Types.BigInt> extends true ? bigint :
111-
IfEquals<PathValueType, BigInt> extends true ? bigint :
112-
PathValueType extends 'bigint' | 'BigInt' | typeof Schema.Types.BigInt | typeof BigInt ? bigint :
113-
PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
114-
IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
115-
PathValueType extends MapConstructor | 'Map' ? Map<string, ResolveRawPathType<Options['of']>> :
116-
IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolveRawPathType<Options['of']>> :
117-
PathValueType extends ArrayConstructor ? any[] :
118-
PathValueType extends typeof Schema.Types.Mixed ? any:
119-
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
120-
IfEquals<PathValueType, {}> extends true ? any:
121-
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
122-
PathValueType extends Record<string, any> ? InferRawDocType<PathValueType> :
123-
unknown;
81+
PathValueType extends ReadonlyArray<infer Item> ?
82+
IfEquals<Item, never, any[], Item extends Schema ?
83+
Array<InferRawDocTypeFromSchema<Item>> :
84+
Item extends Record<TypeKey, any> ?
85+
Item[TypeKey] extends Function | String ?
86+
ObtainRawDocumentPathType<Item, TypeKey>[] :
87+
InferRawDocType<Item>[]:
88+
IsSchemaTypeFromBuiltinClass<Item> extends true ?
89+
ObtainRawDocumentPathType<Item, TypeKey>[] :
90+
IsItRecordAndNotAny<Item> extends true ?
91+
Item extends Record<string, never> ?
92+
ObtainRawDocumentPathType<Item, TypeKey>[] :
93+
Array<InferRawDocType<Item>> :
94+
ObtainRawDocumentPathType<Item, TypeKey>[]
95+
>:
96+
PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> :
97+
IfEquals<PathValueType, Schema.Types.String> extends true ? PathEnumOrString<Options['enum']> :
98+
IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']> :
99+
PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray<any> ? Options['enum'][number] : number :
100+
IfEquals<PathValueType, Schema.Types.Number> extends true ? number :
101+
PathValueType extends DateSchemaDefinition ? NativeDate :
102+
IfEquals<PathValueType, Schema.Types.Date> extends true ? NativeDate :
103+
PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
104+
PathValueType extends BooleanSchemaDefinition ? boolean :
105+
IfEquals<PathValueType, Schema.Types.Boolean> extends true ? boolean :
106+
PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId :
107+
IfEquals<PathValueType, Types.ObjectId> extends true ? Types.ObjectId :
108+
IfEquals<PathValueType, Schema.Types.ObjectId> extends true ? Types.ObjectId :
109+
PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 :
110+
IfEquals<PathValueType, Schema.Types.Decimal128> extends true ? Types.Decimal128 :
111+
IfEquals<PathValueType, Types.Decimal128> extends true ? Types.Decimal128 :
112+
IfEquals<PathValueType, Schema.Types.BigInt> extends true ? bigint :
113+
IfEquals<PathValueType, BigInt> extends true ? bigint :
114+
PathValueType extends 'bigint' | 'BigInt' | typeof Schema.Types.BigInt | typeof BigInt ? bigint :
115+
PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
116+
IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
117+
PathValueType extends MapConstructor | 'Map' ? Map<string, ResolveRawPathType<Options['of']>> :
118+
IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolveRawPathType<Options['of']>> :
119+
PathValueType extends ArrayConstructor ? any[] :
120+
PathValueType extends typeof Schema.Types.Mixed ? any:
121+
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
122+
IfEquals<PathValueType, {}> extends true ? any:
123+
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
124+
PathValueType extends Record<string, any> ? InferRawDocType<PathValueType> :
125+
unknown;
124126
}

0 commit comments

Comments
 (0)