forked from pmndrs/zustand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.test.tsx
175 lines (158 loc) · 4.59 KB
/
types.test.tsx
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import create, {
State,
StateListener,
StateSelector,
PartialState,
EqualityChecker,
StateCreator,
SetState,
GetState,
Subscribe,
Destroy,
UseStore,
StoreApi,
} from '../src/index'
it('can use exposed types', () => {
interface ExampleState {
num: number
numGet: () => number
numGetState: () => number
numSet: (v: number) => void
numSetState: (v: number) => void
}
const listener: StateListener<ExampleState> = (state) => {
if (state) {
const value = state.num * state.numGet() * state.numGetState()
state.numSet(value)
state.numSetState(value)
}
}
const selector: StateSelector<ExampleState, number> = (state) => state.num
const partial: PartialState<ExampleState, 'num' | 'numGet'> = {
num: 2,
numGet: () => 2,
}
const partialFn: PartialState<ExampleState, 'num' | 'numGet'> = (state) => ({
...state,
num: 2,
})
const equalityFn: EqualityChecker<ExampleState> = (state, newState) =>
state !== newState
const storeApi = create<ExampleState>((set, get) => ({
num: 1,
numGet: () => get().num,
numGetState: () => {
// TypeScript can't get the type of storeApi when it trys to enforce the signature of numGetState.
// Need to explicitly state the type of storeApi.getState().num or storeApi type will be type 'any'.
const result: number = storeApi.getState().num
return result
},
numSet: (v) => {
set({ num: v })
},
numSetState: (v) => {
storeApi.setState({ num: v })
},
}))
const useStore = storeApi
const stateCreator: StateCreator<ExampleState> = (set, get) => ({
num: 1,
numGet: () => get().num,
numGetState: () => get().num,
numSet: (v) => {
set({ num: v })
},
numSetState: (v) => {
set({ num: v })
},
})
function checkAllTypes(
_getState: GetState<ExampleState>,
_partialState: PartialState<ExampleState, 'num' | 'numGet'>,
_setState: SetState<ExampleState>,
_state: State,
_stateListener: StateListener<ExampleState>,
_stateSelector: StateSelector<ExampleState, number>,
_storeApi: StoreApi<ExampleState>,
_subscribe: Subscribe<ExampleState>,
_destroy: Destroy,
_equalityFn: EqualityChecker<ExampleState>,
_stateCreator: StateCreator<ExampleState>,
_useStore: UseStore<ExampleState>
) {
expect(true).toBeTruthy()
}
checkAllTypes(
storeApi.getState,
Math.random() > 0.5 ? partial : partialFn,
storeApi.setState,
storeApi.getState(),
listener,
selector,
storeApi,
storeApi.subscribe,
storeApi.destroy,
equalityFn,
stateCreator,
useStore
)
})
type AssertEqual<Type, Expected> = Type extends Expected
? Expected extends Type
? true
: never
: never
it('should have correct (partial) types for setState', () => {
type Count = { count: number }
const store = create<Count>((set) => ({
count: 0,
// @ts-expect-error we shouldn't be able to set count to undefined
a: () => set(() => ({ count: undefined })),
// @ts-expect-error we shouldn't be able to set count to undefined
b: () => set({ count: undefined }),
c: () => set({ count: 1 }),
}))
const setState: AssertEqual<typeof store.setState, SetState<Count>> = true
expect(setState).toEqual(true)
// ok, should not error
store.setState({ count: 1 })
store.setState({})
store.setState(() => undefined)
store.setState((previous) => previous)
// @ts-expect-error type undefined is not assignable to type number
store.setState({ count: undefined })
// @ts-expect-error type undefined is not assignable to type number
store.setState((state) => ({ ...state, count: undefined }))
})
it('should allow for different partial keys to be returnable from setState', () => {
type State = { count: number; something: string }
const store = create<State>(() => ({
count: 0,
something: 'foo',
}))
const setState: AssertEqual<typeof store.setState, SetState<State>> = true
expect(setState).toEqual(true)
// ok, should not error
store.setState((previous) => {
if (previous.count === 0) {
return { count: 1 }
}
return { count: 0 }
})
store.setState<'count', 'something'>((previous) => {
if (previous.count === 0) {
return { count: 1 }
}
if (previous.count === 1) {
return previous
}
return { something: 'foo' }
})
// @ts-expect-error Type '{ something: boolean; count?: undefined; }' is not assignable to type 'State'.
store.setState<'count', 'something'>((previous) => {
if (previous.count === 0) {
return { count: 1 }
}
return { something: true }
})
})