Skip to content

Commit

Permalink
Refactor related types.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad0-0ahmad committed Jun 7, 2022
1 parent e5e7b7a commit 1b21255
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion types/schemaoptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,6 @@ declare module 'mongoose' {
/**
* Virtual paths.
*/
virtuals?: VirtualsSchemaOptionsPropertyType<DocType, TVirtuals, TInstanceMethods>,
virtuals?: SchemaOptionsVirtualsPropertyType<DocType, TVirtuals, TInstanceMethods>,
}
}
13 changes: 8 additions & 5 deletions types/virtuals.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/// <reference path="./utility.d.ts" />

declare module 'mongoose' {
type VirtualPathFunctions<DocType = {}, PathType = unknown, TInstanceMethods = {}> = {
get?: (this: Document<any, any, DocType> & DocType) => PathType;
set?: (this: Document<any, any, DocType> & DocType, ...args: any) => unknown;
type VirtualPathFunctions<DocType = {}, PathValueType = unknown, TInstanceMethods = {}> = {
get?: TVirtualPathFN<DocType, PathValueType, TInstanceMethods, PathValueType>;
set?: TVirtualPathFN<DocType, PathValueType, TInstanceMethods, void>;
options?: VirtualTypeOptions<HydratedDocument<DocType, TInstanceMethods>, DocType>;
};

type VirtualsSchemaOptionsPropertyType<DocType = any, virtualPaths = Record<any, unknown>, TInstanceMethods = {}> = {
[K in keyof virtualPaths]: VirtualPathFunctions<IsItRecordAndNotAny<DocType> extends true ? DocType : any, virtualPaths[K], TInstanceMethods>
type TVirtualPathFN<DocType = {}, PathType = unknown, TInstanceMethods = {}, TReturn = unknown> =
<T = HydratedDocument<DocType, TInstanceMethods>>(this: Document<any, any, DocType> & DocType, value: PathType, virtual: VirtualType<T>, doc: Document<any, any, DocType> & DocType) => TReturn;

type SchemaOptionsVirtualsPropertyType<DocType = any, VirtualPaths = Record<any, unknown>, TInstanceMethods = {}> = {
[K in keyof VirtualPaths]: VirtualPathFunctions<IsItRecordAndNotAny<DocType> extends true ? DocType : any, VirtualPaths[K], TInstanceMethods>
};
}

0 comments on commit 1b21255

Please sign in to comment.