import { IsSchemaTypeFromBuiltinClass, PathEnumOrString, OptionalPaths, RequiredPaths, IsPathRequired, PathAllowsNull } from './inferschematype'; import { Binary, UUID } from 'mongodb'; declare module 'mongoose' { export type InferRawDocTypeFromSchema> = IsItRecordAndNotAny> extends true ? ObtainSchemaGeneric : unknown extends ObtainSchemaGeneric ? Require_id>>> : InferRawDocType>; export type InferRawDocTypeWithout_id< SchemaDefinition, TSchemaOptions extends Record = DefaultSchemaOptions, TTransformOptions = { bufferToBinary: false } > = ApplySchemaOptions<{ [ K in keyof (RequiredPaths & OptionalPaths) ]: IsPathRequired extends true ? ObtainRawDocumentPathType : PathAllowsNull extends true ? ObtainRawDocumentPathType | null : ObtainRawDocumentPathType; }, TSchemaOptions>; export type InferRawDocType< SchemaDefinition, TSchemaOptions extends Record = DefaultSchemaOptions, TTransformOptions = { bufferToBinary: false } > = TSchemaOptions extends { _id: false } ? InferRawDocTypeWithout_id : Require_id>; /** * @summary Allows users to optionally choose their own type for a schema field for stronger typing. * Make sure to check for `any` because `T extends { __rawDocTypeHint: infer U }` will infer `unknown` if T is `any`. */ type RawDocTypeHint = IsAny extends true ? never : T extends { __rawDocTypeHint: infer U } ? U: never; type ResolveDiscriminatorRawPathType = IsAny extends true ? never : TDiscriminators extends Record ? TDiscriminators[keyof TDiscriminators] extends Schema ? MergeType, InferRawDocTypeFromSchema> : never : never; type RawDiscriminatorEnumType = string extends keyof T ? never : number extends keyof T ? never : T extends { type: infer BaseType; discriminators: infer TDiscriminators } ? IsAny extends true ? never : BaseType extends Schema ? ResolveDiscriminatorRawPathType : never : never; /** * @summary Obtains schema Path type. * @description Obtains Path type by separating path type from other options and calling {@link ResolveRawPathType} * @param {PathValueType} PathValueType Document definition path type. * @param {TypeKey} TypeKey A generic refers to document definition. */ type ObtainRawDocumentPathType< PathValueType, TypeKey extends string = DefaultTypeKey, TTransformOptions = { bufferToBinary: false } > = ResolveRawPathType< TypeKey extends keyof PathValueType ? TypeKey extends keyof PathValueType[TypeKey] ? PathValueType : PathValueType[TypeKey] : PathValueType, TypeKey extends keyof PathValueType ? TypeKey extends keyof PathValueType[TypeKey] ? {} : Omit : {}, TypeKey, TTransformOptions, RawDocTypeHint, RawDiscriminatorEnumType, TypeKey extends keyof PathValueType ? false : true >; type neverOrAny = ' ~neverOrAny~'; /** * Same as inferSchemaType, except: * * 1. Replace `Types.DocumentArray` and `Types.Array` with vanilla `Array` * 2. Replace `ObtainDocumentPathType` with `ObtainRawDocumentPathType` * 3. Replace `ResolvePathType` with `ResolveRawPathType` * * @summary Resolve path type by returning the corresponding type. * @param {PathValueType} PathValueType Document definition path type. * @param {Options} Options Document definition path options except path type. * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". * @returns Number, "Number" or "number" will be resolved to number type. */ type ResolveRawPathType< PathValueType, Options extends SchemaTypeOptions = {}, TypeKey extends string = DefaultSchemaOptions['typeKey'], TTransformOptions = { bufferToBinary: false }, TypeHint = never, TDiscriminatorEnumType = never, IsNestedPath extends boolean = false > = IsNotNever extends true ? TypeHint : IsNotNever extends true ? TDiscriminatorEnumType : [PathValueType] extends [neverOrAny] ? PathValueType : PathValueType extends Schema ? IsItRecordAndNotAny extends true ? RawDocType : unknown extends TSchemaDefinition ? TSchemaOptions extends { _id: false } ? FlattenMaps> : Require_id>> : InferRawDocType, TTransformOptions> : PathValueType extends ReadonlyArray ? IfEquals extends true ? any[] : Item extends Schema ? // If Item is a schema, infer its type. Array extends true ? RawDocType : unknown extends TSchemaDefinition ? TSchemaOptions extends { _id: false } ? FlattenMaps> : Require_id>> : InferRawDocType, TTransformOptions>> : TypeKey extends keyof Item ? Item[TypeKey] extends Function | String ? // If Item has a type key that's a string or a callable, it must be a scalar, // so we can directly obtain its path type. ObtainRawDocumentPathType[] : // If the type key isn't callable, then this is an array of objects, in which case // we need to call InferRawDocType to correctly infer its type. Array> : IsSchemaTypeFromBuiltinClass extends true ? ResolveRawPathType[] : IsItRecordAndNotAny extends true ? Item extends Record ? ObtainRawDocumentPathType[] : Array> : ObtainRawDocumentPathType[] : PathValueType extends StringSchemaDefinition ? PathEnumOrString : IfEquals extends true ? PathEnumOrString : PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray ? Options['enum'][number] : number : PathValueType extends DateSchemaDefinition ? NativeDate : PathValueType extends BufferSchemaDefinition ? (TTransformOptions extends { bufferToBinary: true } ? Binary : Buffer) : PathValueType extends BooleanSchemaDefinition ? boolean : PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId : PathValueType extends Decimal128SchemaDefinition ? Types.Decimal128 : PathValueType extends BigintSchemaDefinition ? bigint : PathValueType extends UuidSchemaDefinition ? Types.UUID : PathValueType extends MapSchemaDefinition ? Record> : PathValueType extends DoubleSchemaDefinition ? Types.Double : PathValueType extends UnionSchemaDefinition ? ResolveRawPathType ? Item : never> : PathValueType extends ArrayConstructor ? any[] : PathValueType extends typeof Schema.Types.Mixed ? any : IfEquals extends true ? any : IfEquals extends true ? any : PathValueType extends typeof SchemaType ? PathValueType['prototype'] : PathValueType extends Record ? IsNestedPath extends true ? // Nested path (no type key) - no _id InferRawDocTypeWithout_id : Options extends { _id: false } ? // Subdocument with _id: false InferRawDocTypeWithout_id : // Subdocument with _id (default) InferRawDocType : unknown; }