-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refact some infer-doc-type import lines
- Loading branch information
1 parent
d5cbee3
commit 11b6bed
Showing
6 changed files
with
54 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Schema, InferSchemaType } from 'mongoose'; | ||
|
||
declare module 'mongoose' { | ||
type ObtainDocumentType<DocDefinition, DocType = any> = | ||
DoesDocTypeExist<DocType> extends true ? DocType : { | ||
[K in keyof (RequiredProperties<DocDefinition> & | ||
OptionalProperties<DocDefinition>)]: ObtainDocumentPropertyType<DocDefinition[K]>; | ||
}; | ||
|
||
type InferSchemaType<SchemaType> = SchemaType extends Schema<infer DocType, any, any, any, infer DocDefinition> | ||
? DoesDocTypeExist<DocType> extends true ? DocType : DocDefinition | ||
: unknown; | ||
|
||
type ObtainSchemaGeneric<TSchema, name extends 'DocType' | 'M' | 'TInstanceMethods' | 'TQueryHelpers' | 'DocDefinition' | 'StaticsMethods'> = | ||
TSchema extends Schema<infer DocType, infer M, infer TInstanceMethods, infer TQueryHelpers, infer DocDefinition, infer StaticsMethods> | ||
? { DocType: DocType, M: M, TInstanceMethods: TInstanceMethods, TQueryHelpers: TQueryHelpers, DocDefinition: DocDefinition, StaticsMethods: StaticsMethods }[name] | ||
: never; | ||
} | ||
|
||
type RequiredPropertyKeys<T> = { | ||
[K in keyof T]: T[K] extends { required: true | [true, string | undefined] } ? K : never; | ||
}[keyof T]; | ||
|
||
type RequiredProperties<T> = { | ||
[K in RequiredPropertyKeys<T>]: T[K]; | ||
}; | ||
|
||
type OptionalPropertyKeys<T> = { | ||
[K in keyof T]: T[K] extends { required: true | [true, string | undefined] } ? never : K; | ||
}[keyof T]; | ||
|
||
type OptionalProperties<T> = { | ||
[K in OptionalPropertyKeys<T>]?: T[K]; | ||
}; | ||
|
||
type ResolvePropertyType<PropertyValue> = PropertyValue extends ( | ||
...args: any | ||
) => any | ||
? ReturnType<PropertyValue> | ||
: PropertyValue; | ||
|
||
type ObtainDocumentPropertyType<PropertyValue> = PropertyValue extends Schema<any> | ||
? InferSchemaType<PropertyValue> | ||
: ResolvePropertyType<PropertyValue extends { type: any } | ||
? ResolvePropertyType<PropertyValue['type']> | ||
: PropertyValue | ||
>; | ||
|
||
type DoesDocTypeExist<DocType> = keyof DocType extends string ? true : false; |