-
Notifications
You must be signed in to change notification settings - Fork 0
/
01290-hard-pinia.ts
79 lines (71 loc) · 1.67 KB
/
01290-hard-pinia.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
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
const store = defineStore({
id: '',
state: () => ({
num: 0,
str: '',
}),
getters: {
stringifiedNum() {
// @ts-expect-error
this.num += 1
return this.num.toString()
},
parsedNum() {
return parseInt(this.stringifiedNum)
},
},
actions: {
init() {
this.reset()
this.increment()
},
increment(step = 1) {
this.num += step
},
reset() {
this.num = 0
// @ts-expect-error
this.parsedNum = 0
return true
},
setNum(value: number) {
this.num = value
},
},
})
// @ts-expect-error
store.nopeStateProp
// @ts-expect-error
store.nopeGetter
// @ts-expect-error
store.stringifiedNum()
store.init()
// @ts-expect-error
store.init(0)
store.increment()
store.increment(2)
// @ts-expect-error
store.setNum()
// @ts-expect-error
store.setNum('3')
store.setNum(3)
const r = store.reset()
type _tests = [
Expect<Equal<typeof store.num, number>>,
Expect<Equal<typeof store.str, string>>,
Expect<Equal<typeof store.stringifiedNum, string>>,
Expect<Equal<typeof store.parsedNum, number>>,
Expect<Equal<typeof r, true>>,
]
// ============= Your Code Here =============
type GetFromGetters<Getters> = {
readonly [K in keyof Getters]: Getters[K] extends (...args: any[]) => infer R ? R : never
}
declare function defineStore<State, Getters, Actions>(store: {
id: string,
state: () => State,
getters: Getters & ThisType<Readonly<State> & GetFromGetters<Getters>>,
actions: Actions & ThisType<State & Actions & GetFromGetters<Getters>>,
}): State & GetFromGetters<Getters> & Actions