mongoose 8.17.1 → 8.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/types/index.d.ts CHANGED
@@ -140,9 +140,21 @@ declare module 'mongoose' {
140
140
  ? IfAny<U, T & { _id: Types.ObjectId }, T & Required<{ _id: U }>>
141
141
  : T & { _id: Types.ObjectId };
142
142
 
143
- export type Default__v<T, TSchemaOptions = {}> = TSchemaOptions extends { versionKey: false } ? T : T extends { __v?: infer U }
143
+ export type Default__v<T, TSchemaOptions = {}> = TSchemaOptions extends { versionKey: false }
144
144
  ? T
145
- : T & { __v: number };
145
+ : TSchemaOptions extends { versionKey: infer VK }
146
+ ? (
147
+ // If VK is a *literal* string, add that property
148
+ T & {
149
+ [K in VK as K extends string
150
+ ? (string extends K ? never : K) // drop if wide string
151
+ : never
152
+ ]: number
153
+ }
154
+ )
155
+ : T extends { __v?: infer U }
156
+ ? T
157
+ : T & { __v: number };
146
158
 
147
159
  /** Helper type for getting the hydrated document type from the raw document type. The hydrated document type is what `new MyModel()` returns. */
148
160
  export type HydratedDocument<
@@ -35,6 +35,10 @@ declare module 'mongoose' {
35
35
  TypeKey
36
36
  >;
37
37
 
38
+ type UnionToRawPathType<T extends readonly any[]> = T[number] extends infer U
39
+ ? ResolveRawPathType<U>
40
+ : never;
41
+
38
42
  /**
39
43
  * Same as inferSchemaType, except:
40
44
  *
@@ -109,11 +113,12 @@ declare module 'mongoose' {
109
113
  IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
110
114
  PathValueType extends MapConstructor | 'Map' ? Map<string, ResolveRawPathType<Options['of']>> :
111
115
  IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolveRawPathType<Options['of']>> :
112
- PathValueType extends ArrayConstructor ? any[] :
113
- PathValueType extends typeof Schema.Types.Mixed ? any:
114
- IfEquals<PathValueType, ObjectConstructor> extends true ? any:
115
- IfEquals<PathValueType, {}> extends true ? any:
116
- PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
117
- PathValueType extends Record<string, any> ? InferRawDocType<PathValueType> :
118
- unknown;
116
+ PathValueType extends 'Union' | 'union' | typeof Schema.Types.Union ? Options['of'] extends readonly any[] ? UnionToRawPathType<Options['of']> : never :
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;
119
124
  }
@@ -75,10 +75,7 @@ declare module 'mongoose' {
75
75
 
76
76
  type ApplySchemaOptions<T, O = DefaultSchemaOptions> = ResolveTimestamps<T, O>;
77
77
 
78
- type ResolveTimestamps<T, O> = O extends { methods: any } | { statics: any } | { virtuals: any } | { timestamps?: false } ? T
79
- // For some reason, TypeScript sets all the document properties to unknown
80
- // if we use methods, statics, or virtuals. So avoid inferring timestamps
81
- // if any of these are set for now. See gh-12807
78
+ type ResolveTimestamps<T, O> = O extends { timestamps?: false } ? T
82
79
  : O extends { timestamps: infer TimestampOptions } ? TimestampOptions extends true
83
80
  ? { createdAt: NativeDate; updatedAt: NativeDate; } & T
84
81
  : TimestampOptions extends SchemaTimestampsConfig
@@ -206,6 +203,10 @@ TypeHint<PathValueType>
206
203
  */
207
204
  type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends ReadonlyArray<infer E> ? E : T extends { values: any } ? PathEnumOrString<T['values']> : T extends Record<string, infer V> ? V : string;
208
205
 
206
+ type UnionToType<T extends readonly any[]> = T[number] extends infer U
207
+ ? ResolvePathType<U>
208
+ : never;
209
+
209
210
  type IsSchemaTypeFromBuiltinClass<T> = T extends (typeof String)
210
211
  ? true
211
212
  : T extends (typeof Number)
@@ -317,11 +318,12 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
317
318
  IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
318
319
  PathValueType extends MapConstructor | 'Map' ? Map<string, ResolvePathType<Options['of']>> :
319
320
  IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolvePathType<Options['of']>> :
320
- PathValueType extends ArrayConstructor ? any[] :
321
- PathValueType extends typeof Schema.Types.Mixed ? any:
322
- IfEquals<PathValueType, ObjectConstructor> extends true ? any:
323
- IfEquals<PathValueType, {}> extends true ? any:
324
- PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
325
- PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, { typeKey: TypeKey }> :
326
- unknown,
321
+ PathValueType extends 'Union' | 'union' | typeof Schema.Types.Union ? Options['of'] extends readonly any[] ? UnionToType<Options['of']> : never :
322
+ PathValueType extends ArrayConstructor ? any[] :
323
+ PathValueType extends typeof Schema.Types.Mixed ? any:
324
+ IfEquals<PathValueType, ObjectConstructor> extends true ? any:
325
+ IfEquals<PathValueType, {}> extends true ? any:
326
+ PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
327
+ PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, { typeKey: TypeKey }> :
328
+ unknown,
327
329
  TypeHint>;
@@ -188,7 +188,7 @@ declare module 'mongoose' {
188
188
  _id?: boolean;
189
189
 
190
190
  /** If set, specifies the type of this map's values. Mongoose will cast this map's values to the given type. */
191
- of?: Function | SchemaDefinitionProperty<any>;
191
+ of?: Function | SchemaDefinitionProperty<any> | (Function | SchemaDefinitionProperty<any>)[];
192
192
 
193
193
  /** If true, uses Mongoose's default `_id` settings. Only allowed for ObjectIds */
194
194
  auto?: boolean;
@@ -569,6 +569,13 @@ declare module 'mongoose' {
569
569
  static defaultOptions: Record<string, any>;
570
570
  }
571
571
 
572
+ class Union extends SchemaType {
573
+ static schemaName: 'Union';
574
+
575
+ /** Default options for this SchemaType */
576
+ static defaultOptions: Record<string, any>;
577
+ }
578
+
572
579
  class UUID extends SchemaType {
573
580
  /** This schema type's name, to defend against minifiers that mangle function names. */
574
581
  static schemaName: 'UUID';