1- import { Schema , InferSchemaType , SchemaType , SchemaTypeOptions , TypeKeyBaseType , Types , NumberSchemaDefinition , StringSchemaDefinition , BooleanSchemaDefinition , DateSchemaDefinition } from 'mongoose' ;
1+ import {
2+ Schema ,
3+ InferSchemaType ,
4+ SchemaType ,
5+ SchemaTypeOptions ,
6+ TypeKeyBaseType ,
7+ Types ,
8+ NumberSchemaDefinition ,
9+ StringSchemaDefinition ,
10+ BooleanSchemaDefinition ,
11+ DateSchemaDefinition ,
12+ ObtainDocumentType ,
13+ DefaultTypeKey ,
14+ ObjectIdSchemaDefinition
15+ } from 'mongoose' ;
216
317declare module 'mongoose' {
418 /**
@@ -75,9 +89,9 @@ type IsPathRequired<P, TypeKey extends TypeKeyBaseType> =
7589 ? P extends { default : undefined }
7690 ? false
7791 : true
78- : P extends ( Record < TypeKey , NumberSchemaDefinition | StringSchemaDefinition | BooleanSchemaDefinition | DateSchemaDefinition > )
79- ? P extends { default : ResolvePathType < P [ TypeKey ] > }
80- ? true
92+ : P extends ( Record < TypeKey , any > )
93+ ? P extends { default : any }
94+ ? IfEquals < P [ 'default' ] , undefined , false , true >
8195 : false
8296 : false ;
8397
@@ -138,7 +152,8 @@ type ObtainDocumentPathType<PathValueType, TypeKey extends TypeKeyBaseType> = Pa
138152 ? InferSchemaType < PathValueType >
139153 : ResolvePathType <
140154 PathValueType extends PathWithTypePropertyBaseType < TypeKey > ? PathValueType [ TypeKey ] : PathValueType ,
141- PathValueType extends PathWithTypePropertyBaseType < TypeKey > ? Omit < PathValueType , TypeKey > : { }
155+ PathValueType extends PathWithTypePropertyBaseType < TypeKey > ? Omit < PathValueType , TypeKey > : { } ,
156+ TypeKey
142157 > ;
143158
144159/**
@@ -151,23 +166,24 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends (
151166 * @summary Resolve path type by returning the corresponding type.
152167 * @param {PathValueType } PathValueType Document definition path type.
153168 * @param {Options } Options Document definition path options except path type.
154- * @returns Number, "Number" or "number" will be resolved to string type.
169+ * @param {TypeKey } TypeKey A generic of literal string type."Refers to the property used for path type definition".
170+ * @returns Number, "Number" or "number" will be resolved to number type.
155171 */
156- type ResolvePathType < PathValueType , Options extends SchemaTypeOptions < PathValueType > = { } > =
172+ type ResolvePathType < PathValueType , Options extends SchemaTypeOptions < PathValueType > = { } , TypeKey extends TypeKeyBaseType = DefaultTypeKey > =
157173 PathValueType extends Schema ? InferSchemaType < PathValueType > :
158- PathValueType extends ( infer Item ) [ ] ? IfEquals < Item , never , any , ResolvePathType < Item > > [ ] :
174+ PathValueType extends ( infer Item ) [ ] ? IfEquals < Item , never , any [ ] , Item extends Schema ? Types . DocumentArray < ResolvePathType < Item > > : ResolvePathType < Item > [ ] > :
159175 PathValueType extends StringSchemaDefinition ? PathEnumOrString < Options [ 'enum' ] > :
160176 PathValueType extends NumberSchemaDefinition ? number :
161177 PathValueType extends DateSchemaDefinition ? Date :
162178 PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema . Types . Buffer ? Buffer :
163179 PathValueType extends BooleanSchemaDefinition ? boolean :
164- PathValueType extends 'objectId' | 'ObjectId' | typeof Schema . Types . ObjectId ? Types . ObjectId :
180+ PathValueType extends ObjectIdSchemaDefinition ? Types . ObjectId :
165181 PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema . Types . Decimal128 ? Types . Decimal128 :
166182 PathValueType extends MapConstructor ? Map < string , ResolvePathType < Options [ 'of' ] > > :
167183 PathValueType extends ArrayConstructor ? any [ ] :
168184 PathValueType extends typeof Schema . Types . Mixed ? any :
169185 IfEquals < PathValueType , ObjectConstructor > extends true ? any :
170186 IfEquals < PathValueType , { } > extends true ? any :
171187 PathValueType extends typeof SchemaType ? PathValueType [ 'prototype' ] :
172- PathValueType extends { } ? PathValueType :
188+ PathValueType extends Record < string , any > ? ObtainDocumentType < PathValueType , any , TypeKey > :
173189 unknown ;
0 commit comments