1 | declare module 'mongoose' {
|
2 | type IfAny<IFTYPE, THENTYPE, ELSETYPE = IFTYPE> = 0 extends (1 & IFTYPE) ? THENTYPE : ELSETYPE;
|
3 | type IfUnknown<IFTYPE, THENTYPE> = unknown extends IFTYPE ? THENTYPE : IFTYPE;
|
4 |
|
5 | type WithLevel1NestedPaths<T, K extends keyof T = keyof T> = {
|
6 | [P in K | NestedPaths<Required<T>, K>]: P extends K
|
7 | ? T[P]
|
8 | : P extends `${infer Key}.${infer Rest}`
|
9 | ? Key extends keyof T
|
10 | ? Rest extends keyof NonNullable<T[Key]>
|
11 | ? NonNullable<T[Key]>[Rest]
|
12 | : never
|
13 | : never
|
14 | : never;
|
15 | };
|
16 |
|
17 | type NestedPaths<T, K extends keyof T> = K extends string
|
18 | ? T[K] extends Record<string, any> | null | undefined
|
19 | ? `${K}.${keyof NonNullable<T[K]> & string}`
|
20 | : never
|
21 | : never;
|
22 |
|
23 | type WithoutUndefined<T> = T extends undefined ? never : T;
|
24 |
|
25 | /**
|
26 | * @summary Removes keys from a type
|
27 | * @description It helps to exclude keys from a type
|
28 | * @param {T} T A generic type to be checked.
|
29 | * @param {K} K Keys from T that are to be excluded from the generic type
|
30 | * @returns T with the keys in K excluded
|
31 | */
|
32 | type ExcludeKeys<T, K extends keyof T> = {
|
33 | [P in keyof T as P extends K ? never : P]: T[P];
|
34 | };
|
35 |
|
36 | type Unpacked<T> = T extends (infer U)[] ?
|
37 | U :
|
38 | T extends ReadonlyArray<infer U> ? U : T;
|
39 |
|
40 | type UnpackedIntersection<T, U> = T extends null ? null : T extends (infer A)[]
|
41 | ? (Omit<A, keyof U> & U)[]
|
42 | : keyof U extends never
|
43 | ? T
|
44 | : Omit<T, keyof U> & U;
|
45 |
|
46 | type MergeType<A, B> = Omit<A, keyof B> & B;
|
47 |
|
48 | /**
|
49 | * @summary Converts Unions to one record "object".
|
50 | * @description It makes intellisense dialog box easier to read as a single object instead of showing that in multiple object unions.
|
51 | * @param {T} T The type to be converted.
|
52 | */
|
53 | type FlatRecord<T> = { [K in keyof T]: T[K] };
|
54 |
|
55 | /**
|
56 | * @summary Checks if a type is "Record" or "any".
|
57 | * @description It Helps to check if user has provided schema type "EnforcedDocType"
|
58 | * @param {T} T A generic type to be checked.
|
59 | * @returns true if {@link T} is Record OR false if {@link T} is of any type.
|
60 | */
|
61 | type IsItRecordAndNotAny<T> = IfEquals<T, any, false, T extends Record<any, any> ? true : false>;
|
62 |
|
63 | /**
|
64 | * @summary Checks if two types are identical.
|
65 | * @param {T} T The first type to be compared with {@link U}.
|
66 | * @param {U} U The seconde type to be compared with {@link T}.
|
67 | * @param {Y} Y A type to be returned if {@link T} & {@link U} are identical.
|
68 | * @param {N} N A type to be returned if {@link T} & {@link U} are not identical.
|
69 | */
|
70 | type IfEquals<T, U, Y = true, N = false> =
|
71 | (<G>() => G extends T ? 1 : 0) extends
|
72 | (<G>() => G extends U ? 1 : 0) ? Y : N;
|
73 |
|
74 | /**
|
75 | * @summary Extracts 'this' parameter from a function, if it exists. Otherwise, returns fallback.
|
76 | * @param {T} T Function type to extract 'this' parameter from.
|
77 | * @param {F} F Fallback type to return if 'this' parameter does not exist.
|
78 | */
|
79 | type ThisParameter<T, F> = T extends { (this: infer This): void }
|
80 | ? This
|
81 | : F;
|
82 |
|
83 | /**
|
84 | * @summary Decorates all functions in an object with 'this' parameter.
|
85 | * @param {T} T Object with functions as values to add 'D' parameter to as 'this'. {@link D}
|
86 | * @param {D} D The type to be added as 'this' parameter to all functions in {@link T}.
|
87 | */
|
88 | type AddThisParameter<T, D> = {
|
89 | [K in keyof T]: T[K] extends (...args: infer A) => infer R
|
90 | ? ThisParameter<T[K], unknown> extends unknown
|
91 | ? (this: D, ...args: A) => R
|
92 | : T[K]
|
93 | : T[K];
|
94 | };
|
95 |
|
96 | }
|