Skip to content

Commit

Permalink
Resolve Mixed type as any type.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad0-0ahmad committed Apr 19, 2022
1 parent 9960dd2 commit 1347304
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
14 changes: 7 additions & 7 deletions test/types/schema.test.ts
Expand Up @@ -362,20 +362,20 @@ export function autoTypedSchema() {
boolean2?: boolean;
boolean3?: boolean;
boolean4?: boolean;
mixed1?: Schema.Types.Mixed;
mixed2?: Schema.Types.Mixed;
mixed3?: Schema.Types.Mixed;
mixed1?: any;
mixed2?: any;
mixed3?: any;
objectId1?: Schema.Types.ObjectId;
objectId2?: Schema.Types.ObjectId;
objectId3?: Schema.Types.ObjectId;
customSchema?: Int8;
map1?: Map<string, string>;
map2?: Map<string, number>;
array1?: string[];
array2?: Schema.Types.Mixed[];
array3?: Schema.Types.Mixed[];
array4?: Schema.Types.Mixed[];
array5?: Schema.Types.Mixed[];
array2?: any[];
array3?: any[];
array4?: any[];
array5?: any[];
};

const TestSchema = new Schema({
Expand Down
31 changes: 15 additions & 16 deletions types/inferschematype.d.ts
Expand Up @@ -23,9 +23,7 @@ declare module 'mongoose' {
* // result
* type UserType = {userName?: string}
*/
type InferSchemaType<SchemaType> = SchemaType extends Schema<infer EnforcedDocType>
? IsItRecordAndNotAny<EnforcedDocType> extends true ? EnforcedDocType : ObtainSchemaGeneric<SchemaType, 'DocType'>
: unknown;
type InferSchemaType<SchemaType> = ObtainSchemaGeneric<SchemaType, 'DocType'> ;

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

/**
Expand Down Expand Up @@ -139,16 +137,17 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends (
* @returns Number, "Number" or "number" will be resolved to string type.
*/
type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}> =
PathValueType extends (infer Item)[] ? IfEquals<Item, never, Schema.Types.Mixed, ResolvePathType<Item>>[] :
PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString<Options['enum']> :
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<string, ResolvePathType<Options['of']>> :
PathValueType extends ArrayConstructor ? Schema.Types.Mixed[] :
keyof PathValueType extends keyof {} ? Schema.Types.Mixed :
PathValueType extends (infer Item)[] ? IfEquals<Item, never, any, ResolvePathType<Item>>[] :
PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString<Options['enum']> :
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 MapConstructor ? Map<string, ResolvePathType<Options['of']>> :
PathValueType extends ArrayConstructor ? any[] :
PathValueType extends typeof Schema.Types.Mixed ? any:
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
IfEquals<PathValueType, {}> extends true ? any:
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
unknown;
unknown;

0 comments on commit 1347304

Please sign in to comment.