diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 83f6f471972..8fa078e02a7 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -1,5 +1,6 @@ import { Schema, Document, SchemaDefinition, Model, Types, InferSchemaType, SchemaType, Query, HydratedDocument } from 'mongoose'; import { expectType, expectError, expectAssignable } from 'tsd'; +import { IsTAny } from '../../types/inferschematype'; enum Genre { Action, @@ -359,6 +360,10 @@ export function autoTypedSchema() { customSchema?: Int8; map1?: Map; map2?: Map; + array1?: string[]; + array2?: Schema.Types.Mixed[]; + array3?: Schema.Types.Mixed[]; + array4?: Schema.Types.Mixed[]; }; const TestSchema = new Schema({ @@ -390,7 +395,11 @@ export function autoTypedSchema() { objectId3: 'objectId', customSchema: Int8, map1: { type: Map, of: String }, - map2: { type: Map, of: Number } + map2: { type: Map, of: Number }, + array1: { type: [String] }, + array2: { type: Array }, + array3: { type: [Schema.Types.Mixed] }, + array4: { type: [{}] } }); type InferredTestSchemaType = InferSchemaType; diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index efeb84f6afd..bce112efc3b 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -23,8 +23,8 @@ declare module 'mongoose' { * // result * type UserType = {userName?: string} */ - type InferSchemaType = SchemaType extends Schema - ? IsItRecordAndNotAny extends true ? DocType : ObtainSchemaGeneric + type InferSchemaType = SchemaType extends Schema + ? IsItRecordAndNotAny extends true ? EnforcedDocType : ObtainSchemaGeneric : unknown; /** @@ -51,7 +51,9 @@ declare module 'mongoose' { * @param {T} T A generic type to be checked. * @returns true if {@link T} is Record OR false if {@link T} is of any type. */ -type IsItRecordAndNotAny = T extends any[] ? false : T extends Record ? true : false; +type IsItRecordAndNotAny = IsTAny extends true ? false : T extends Record ? true : false; + +type IsTAny = keyof any extends keyof T ? (unknown extends T ? true : false) : false; /** * @summary Required path base type. @@ -128,15 +130,17 @@ type PathEnumOrString['enum']> = T extends ( * @returns Number, "Number" or "number" will be resolved to string type. */ type ResolvePathType = {}> = -PathValueType extends (infer Item)[] ? ResolvePathType[] : - PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString : - PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number : - PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date : - PathValueType extends BufferConstructor | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer : - PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean : - PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Schema.Types.ObjectId : - PathValueType extends ObjectConstructor | typeof Schema.Types.Mixed ? Schema.Types.Mixed : - keyof PathValueType extends never ? Schema.Types.Mixed : - PathValueType extends MapConstructor ? Map> : - PathValueType extends typeof SchemaType ? PathValueType['prototype'] : - unknown; \ No newline at end of file + IsTAny extends true ? Schema.Types.Mixed: + PathValueType extends (infer Item)[] ? ResolvePathType[] : + PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString : + PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number : + PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date : + PathValueType extends BufferConstructor | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer : + PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean : + PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Schema.Types.ObjectId : + PathValueType extends ObjectConstructor | typeof Schema.Types.Mixed ? Schema.Types.Mixed : + PathValueType extends MapConstructor ? Map> : + PathValueType extends ArrayConstructor ? Schema.Types.Mixed[] : + keyof PathValueType extends keyof {} ? Schema.Types.Mixed : + PathValueType extends typeof SchemaType ? PathValueType['prototype'] : + unknown;