Skip to content

Commit 1b21255

Browse files
Refactor related types.
1 parent e5e7b7a commit 1b21255

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

types/schemaoptions.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,6 @@ declare module 'mongoose' {
206206
/**
207207
* Virtual paths.
208208
*/
209-
virtuals?: VirtualsSchemaOptionsPropertyType<DocType, TVirtuals, TInstanceMethods>,
209+
virtuals?: SchemaOptionsVirtualsPropertyType<DocType, TVirtuals, TInstanceMethods>,
210210
}
211211
}

types/virtuals.d.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
/// <reference path="./utility.d.ts" />
22

33
declare module 'mongoose' {
4-
type VirtualPathFunctions<DocType = {}, PathType = unknown, TInstanceMethods = {}> = {
5-
get?: (this: Document<any, any, DocType> & DocType) => PathType;
6-
set?: (this: Document<any, any, DocType> & DocType, ...args: any) => unknown;
4+
type VirtualPathFunctions<DocType = {}, PathValueType = unknown, TInstanceMethods = {}> = {
5+
get?: TVirtualPathFN<DocType, PathValueType, TInstanceMethods, PathValueType>;
6+
set?: TVirtualPathFN<DocType, PathValueType, TInstanceMethods, void>;
77
options?: VirtualTypeOptions<HydratedDocument<DocType, TInstanceMethods>, DocType>;
88
};
99

10-
type VirtualsSchemaOptionsPropertyType<DocType = any, virtualPaths = Record<any, unknown>, TInstanceMethods = {}> = {
11-
[K in keyof virtualPaths]: VirtualPathFunctions<IsItRecordAndNotAny<DocType> extends true ? DocType : any, virtualPaths[K], TInstanceMethods>
10+
type TVirtualPathFN<DocType = {}, PathType = unknown, TInstanceMethods = {}, TReturn = unknown> =
11+
<T = HydratedDocument<DocType, TInstanceMethods>>(this: Document<any, any, DocType> & DocType, value: PathType, virtual: VirtualType<T>, doc: Document<any, any, DocType> & DocType) => TReturn;
12+
13+
type SchemaOptionsVirtualsPropertyType<DocType = any, VirtualPaths = Record<any, unknown>, TInstanceMethods = {}> = {
14+
[K in keyof VirtualPaths]: VirtualPathFunctions<IsItRecordAndNotAny<DocType> extends true ? DocType : any, VirtualPaths[K], TInstanceMethods>
1215
};
1316
}

0 commit comments

Comments
 (0)