1
1
import { around } from "monkey-around"
2
2
import { Plugin } from "obsidian"
3
3
4
+ // Is any
5
+ type IsAny < T > = 0 extends 1 & T ? true : false
6
+ type NotAny < T > = IsAny < T > extends true ? never : T
7
+
4
8
// All keys in T that are functions
5
9
type FunctionKeys < T > = {
6
10
[ K in keyof T ] : T [ K ] extends ( ...args : any [ ] ) => any ? K : never
@@ -11,21 +15,23 @@ type KeyFunction<T, K extends FunctionKeys<T>> =
11
15
T [ K ] extends ( ...args : any [ ] ) => any ? T [ K ] : never
12
16
13
17
// The type of a patch function for key K in T
14
- type KeyFunctionReplacement < T , K extends FunctionKeys < T > > =
15
- ( this : T , ...args : Parameters < KeyFunction < T , K > > ) => ReturnType < KeyFunction < T , K > >
18
+ type KeyFunctionReplacement < T , K extends FunctionKeys < T > , R extends ReturnType < KeyFunction < T , K > > > =
19
+ ( this : T , ...args : Parameters < KeyFunction < T , K > > ) => IsAny < ReturnType < KeyFunction < T , K > > > extends false
20
+ ? ReturnType < KeyFunction < T , K > > & NotAny < R >
21
+ : any
16
22
17
23
// The wrapper of a patch function for key K in T
18
- type PatchFunctionWrapper < T , K extends FunctionKeys < T > > =
19
- ( next : KeyFunction < T , K > ) => KeyFunctionReplacement < T , K >
24
+ type PatchFunctionWrapper < T , K extends FunctionKeys < T > , R extends ReturnType < KeyFunction < T , K > > > =
25
+ ( next : KeyFunction < T , K > ) => KeyFunctionReplacement < T , K , R >
20
26
21
27
// The object of patch functions for T
22
28
type FunctionPatchObject < T > = {
23
- [ K in FunctionKeys < T > ] ?: PatchFunctionWrapper < T , K > & { __overrideExisting ?: boolean }
29
+ [ K in FunctionKeys < T > ] ?: PatchFunctionWrapper < T , K , ReturnType < KeyFunction < T , K > > > & { __overrideExisting ?: boolean }
24
30
}
25
31
26
32
export default class PatchHelper {
27
- static OverrideExisting < T , K extends FunctionKeys < T > > (
28
- fn : PatchFunctionWrapper < T , K > & { __overrideExisting ?: boolean }
33
+ static OverrideExisting < T , K extends FunctionKeys < T > , R extends ReturnType < KeyFunction < T , K > > > (
34
+ fn : PatchFunctionWrapper < T , K , R > & { __overrideExisting ?: boolean }
29
35
) { return Object . assign ( fn , { __overrideExisting : true } ) }
30
36
31
37
static patchPrototype < T > (
0 commit comments