-
Notifications
You must be signed in to change notification settings - Fork 1
/
ts.ts
99 lines (87 loc) · 1.45 KB
/
ts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const modules = {
user: {
namespaced: true,
state: {},
getters: {
isLoading: true,
},
actions: {},
mutations: {},
},
other: {
namespaced: true,
state: {},
getters: { count: () => 1 },
actions: {},
mutations: {},
},
};
// 原始类型
// {
// user: {
// namespaced: true,
// state: {},
// getters: {},
// actions: {},
// mutations: {}
// }
// }
// 目标类型
// {
// "user/isLoading": string,
// ...
// }
/**
* {
* "user": {
* "isLoading": boolean
* },
* "other": {
* ...
* }
* }
*/
type GetGetters<M> = {
[K in keyof M]: M[K] extends { getters: infer G } ? G : unknown;
};
/**
* {
* user: {
* isLoading: () => string
* }
* }
*/
type MGetters = GetGetters<typeof modules>;
/**
* {
* "user": "user/isLoading"
* }
*/
type GetSpliceKey<K, M> = `${K & string}/${M & string}`;
type GetSpliceKeys<M> = {
[K in keyof M]: GetSpliceKey<K, keyof M[K]>;
}[keyof M];
type s = GetSpliceKeys<MGetters>;
/**
* {
* "user/isLoading": () => string
* }
*
* [K in GetSpliceKeys<M>] === "user/isLoading" | ...
*
*
* M === MGetters
*/
type GetFunc<M, A, B> = M[A & keyof M][B & keyof M[A & keyof M]];
type GetSpliceObj<M> = {
[K in GetSpliceKeys<M>]: K extends `${infer A}/${infer B}`
? GetFunc<M, A, B>
: unknown;
};
/**
* {
* "user/isLoading": string
* }
*/
type Getters = GetSpliceObj<MGetters>;
export { Getters };