Skip to content

Commit

Permalink
Create and implement Extends to avoid circular ref error.
Browse files Browse the repository at this point in the history
  • Loading branch information
ts-benchmark committed Aug 5, 2022
1 parent 5dec88b commit d7456e9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
7 changes: 3 additions & 4 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import {
IfEquals,
SchemaOptions,
DefaultSchemaOptions,
MergeType,
FlatRecord
MergeType
} from 'mongoose';

declare module 'mongoose' {
Expand Down Expand Up @@ -162,7 +161,7 @@ type OptionalPaths<T, TypeKey extends string > = {
* @param {PathValueType} PathValueType Document definition path type.
* @param {TypeKey} TypeKey A generic refers to document definition.
*/
type ObtainDocumentPathType<PathValueType, TypeKey extends string > = PathValueType extends Schema
type ObtainDocumentPathType<PathValueType, TypeKey extends string > = IfExtends<PathValueType, Schema> extends true
? InferSchemaType<PathValueType>
: ResolvePathType<
PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? PathValueType[TypeKey] : PathValueType,
Expand All @@ -184,7 +183,7 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends (
* @returns Number, "Number" or "number" will be resolved to number type.
*/
type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends SchemaOptions['typeKey'] = DefaultSchemaOptions['typeKey']> =
PathValueType extends Schema ? InferSchemaType<PathValueType> :
IfExtends<PathValueType, Schema> extends true ? InferSchemaType<PathValueType> :
PathValueType extends (infer Item)[] ? IfEquals<Item, never, any[], Item extends Schema ? Types.DocumentArray<ResolvePathType<Item>> : ResolvePathType<Item>[]> :
PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> :
PathValueType extends NumberSchemaDefinition ? number :
Expand Down
7 changes: 6 additions & 1 deletion types/utility.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@ type IsItRecordAndNotAny<T> = IfEquals<T, any, false, T extends Record<any, any>
type IfEquals<T, U, Y = true, N = false> =
(<G>() => G extends T ? 1 : 0) extends
(<G>() => G extends U ? 1 : 0) ? Y : N;

}

type IfExtends<T, E, Y = true, N = false> = T extends infer IT
? IT extends E
? Y
: N
: never;

0 comments on commit d7456e9

Please sign in to comment.