Skip to content

Commit

Permalink
馃悰fix: resolves type of nested paths that isn't a schema
Browse files Browse the repository at this point in the history
  • Loading branch information
iammola committed Jun 29, 2022
1 parent e3679a8 commit 79c9ce4
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions types/inferschematype.d.ts
@@ -1,4 +1,4 @@
import { Schema, Types, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType } from 'mongoose';
import { Schema, Types, InferSchemaType, ObtainDocumentType, SchemaDefinitionProperty, SchemaType, SchemaTypeOptions, TypeKeyBaseType } from 'mongoose';

declare module 'mongoose' {
/**
Expand Down Expand Up @@ -149,18 +149,19 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends (
* @returns Number, "Number" or "number" will be resolved to string type.
*/
type ResolvePathType<TypeKey extends TypeKeyBaseType, PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}> =
PathValueType extends (infer Item)[] ? ObtainDocumentPathType<Item, TypeKey>[] :
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 typeof Buffer | '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 ? Types.ObjectId :
PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 :
PathValueType extends MapConstructor ? Map<string, ResolvePathType<TypeKey, 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;
PathValueType extends (infer Item)[] ? IfEquals<Item, never, any, ObtainDocumentPathType<Item, TypeKey>>[] :
PathValueType extends { [K: string]: SchemaDefinitionProperty } ? ObtainDocumentType<PathValueType, any, TypeKey> :
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 typeof Buffer | '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 ? Types.ObjectId :
PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 :
PathValueType extends MapConstructor ? Map<string, ResolvePathType<TypeKey, 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;

0 comments on commit 79c9ce4

Please sign in to comment.