Skip to content

Commit 338a9e5

Browse files
Resolve Mixed type as any type.
1 parent d3e3b2f commit 338a9e5

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

test/types/schema.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,20 +362,20 @@ export function autoTypedSchema() {
362362
boolean2?: boolean;
363363
boolean3?: boolean;
364364
boolean4?: boolean;
365-
mixed1?: Schema.Types.Mixed;
366-
mixed2?: Schema.Types.Mixed;
367-
mixed3?: Schema.Types.Mixed;
365+
mixed1?: any;
366+
mixed2?: any;
367+
mixed3?: any;
368368
objectId1?: Schema.Types.ObjectId;
369369
objectId2?: Schema.Types.ObjectId;
370370
objectId3?: Schema.Types.ObjectId;
371371
customSchema?: Int8;
372372
map1?: Map<string, string>;
373373
map2?: Map<string, number>;
374374
array1?: string[];
375-
array2?: Schema.Types.Mixed[];
376-
array3?: Schema.Types.Mixed[];
377-
array4?: Schema.Types.Mixed[];
378-
array5?: Schema.Types.Mixed[];
375+
array2?: any[];
376+
array3?: any[];
377+
array4?: any[];
378+
array5?: any[];
379379
};
380380

381381
const TestSchema = new Schema({

types/inferschematype.d.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ declare module 'mongoose' {
2323
* // result
2424
* type UserType = {userName?: string}
2525
*/
26-
type InferSchemaType<SchemaType> = SchemaType extends Schema<infer EnforcedDocType>
27-
? IsItRecordAndNotAny<EnforcedDocType> extends true ? EnforcedDocType : ObtainSchemaGeneric<SchemaType, 'DocType'>
28-
: unknown;
26+
type InferSchemaType<SchemaType> = ObtainSchemaGeneric<SchemaType, 'DocType'> ;
2927

3028
/**
3129
* @summary Obtains schema Generic type by using generic alias.
@@ -83,7 +81,7 @@ type PathWithTypePropertyBaseType<TypeKey extends TypeKeyBaseType> = { [k in Typ
8381
* @returns required paths keys of document definition.
8482
*/
8583
type RequiredPathKeys<T> = {
86-
[K in keyof T]: T[K] extends RequiredPathBaseType ? K : never;
84+
[K in keyof T]: T[K] extends RequiredPathBaseType ? IfEquals<T[K], any, never, K> : never;
8785
}[keyof T];
8886

8987
/**
@@ -139,16 +137,17 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends (
139137
* @returns Number, "Number" or "number" will be resolved to string type.
140138
*/
141139
type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}> =
142-
PathValueType extends (infer Item)[] ? IfEquals<Item, never, Schema.Types.Mixed, ResolvePathType<Item>>[] :
143-
PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString<Options['enum']> :
144-
PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number :
145-
PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date :
146-
PathValueType extends BufferConstructor | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
147-
PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean :
148-
PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Schema.Types.ObjectId :
149-
PathValueType extends ObjectConstructor | typeof Schema.Types.Mixed ? Schema.Types.Mixed :
150-
PathValueType extends MapConstructor ? Map<string, ResolvePathType<Options['of']>> :
151-
PathValueType extends ArrayConstructor ? Schema.Types.Mixed[] :
152-
keyof PathValueType extends keyof {} ? Schema.Types.Mixed :
140+
PathValueType extends (infer Item)[] ? IfEquals<Item, never, any, ResolvePathType<Item>>[] :
141+
PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString<Options['enum']> :
142+
PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number :
143+
PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date :
144+
PathValueType extends BufferConstructor | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
145+
PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean :
146+
PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Schema.Types.ObjectId :
147+
PathValueType extends MapConstructor ? Map<string, ResolvePathType<Options['of']>> :
148+
PathValueType extends ArrayConstructor ? any[] :
149+
PathValueType extends typeof Schema.Types.Mixed ? any:
150+
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
151+
IfEquals<PathValueType, {}> extends true ? any:
153152
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
154-
unknown;
153+
unknown;

0 commit comments

Comments
 (0)