-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathhooks.tsx
253 lines (217 loc) · 6.16 KB
/
hooks.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* eslint-disable @typescript-eslint/no-unused-vars, no-inner-declarations */
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import type { Store, Dispatch, AnyAction } from '@reduxjs/toolkit'
import { configureStore } from '@reduxjs/toolkit'
import type {
ReactReduxContextValue,
Selector,
TypedUseSelectorHook,
} from '../../src/index'
import {
connect,
ConnectedProps,
Provider,
DispatchProp,
MapStateToProps,
ReactReduxContext,
shallowEqual,
MapDispatchToProps,
useDispatch,
useSelector,
useStore,
createDispatchHook,
createSelectorHook,
createStoreHook,
} from '../../src/index'
import type { AppDispatch, RootState } from './counterApp'
import {
CounterState,
counterSlice,
increment,
incrementAsync,
AppThunk,
fetchCount,
} from './counterApp'
import { expectType, expectExactType } from '../typeTestHelpers'
function preTypedHooksSetup() {
// Standard hooks setup
const useAppDispatch = () => useDispatch<AppDispatch>()
const useAppSelector: TypedUseSelectorHook<RootState> = useSelector
function CounterComponent() {
const dispatch = useAppDispatch()
return (
<button
onClick={() => {
dispatch(incrementAsync(1))
}}
/>
)
}
}
function TestSelector() {
interface OwnProps {
key?: string | undefined
}
interface State {
key: string
}
const simpleSelect: Selector<State, string> = (state: State) => state.key
const notSimpleSelect: Selector<State, string, OwnProps> = (
state: State,
ownProps: OwnProps
) => ownProps.key || state.key
const ownProps = {}
const state = { key: 'value' }
simpleSelect(state)
notSimpleSelect(state, ownProps)
// @ts-expect-error
simpleSelect(state, ownProps)
// @ts-expect-error
notSimpleSelect(state)
}
function testShallowEqual() {
// @ts-expect-error
shallowEqual()
// @ts-expect-error
shallowEqual('a')
shallowEqual('a', 'a')
shallowEqual({ test: 'test' }, { test: 'test' })
shallowEqual({ test: 'test' }, 'a')
const x: boolean = shallowEqual('a', 'a')
type TestState = { stateProp: string }
// Additionally, it should infer its type from arguments and not become `any`
const selected1 = useSelector(
(state: TestState) => state.stateProp,
shallowEqual
)
expectExactType<string>(selected1)
const useAppSelector: TypedUseSelectorHook<TestState> = useSelector
const selected2 = useAppSelector((state) => state.stateProp, shallowEqual)
expectExactType<string>(selected2)
}
function testUseDispatch() {
const actionCreator = (selected: boolean) => ({
type: 'ACTION_CREATOR',
payload: selected,
})
const thunkActionCreator = (selected: boolean) => {
return (dispatch: Dispatch) => {
return dispatch(actionCreator(selected))
}
}
const dispatch = useDispatch()
dispatch(actionCreator(true))
// @ts-expect-error
dispatch(thunkActionCreator(true))
// @ts-expect-error
dispatch(true)
const store = configureStore({
reducer: (state = 0) => state,
})
type AppDispatch = typeof store.dispatch
// tslint:disable-next-line:no-unnecessary-callback-wrapper (required for the generic parameter)
const useThunkDispatch = () => useDispatch<AppDispatch>()
const thunkDispatch = useThunkDispatch()
const result: ReturnType<typeof actionCreator> = thunkDispatch(
thunkActionCreator(true)
)
}
function testUseSelector() {
interface State {
counter: number
active: string
}
const selector = (state: State) => {
return {
counter: state.counter,
active: state.active,
}
}
const { counter, active } = useSelector(selector)
counter === 1
// @ts-expect-error
counter === '321'
active === 'hi'
// @ts-expect-error
active === {}
// @ts-expect-error
const { extraneous } = useSelector(selector)
useSelector(selector)
// @ts-expect-error
useSelector(selector, 'a')
useSelector(selector, (l, r) => l === r)
useSelector(selector, (l, r) => {
expectType<{ counter: number; active: string }>(l)
return l === r
})
const correctlyInferred: State = useSelector(selector, shallowEqual)
const correctlyInferred2: State = useSelector(selector, {
equalityFn: shallowEqual,
stabilityCheck: 'never',
})
// @ts-expect-error
const inferredTypeIsNotString: string = useSelector(selector, shallowEqual)
const compare = (_l: number, _r: number) => true
useSelector(() => 1, compare)
const compare2 = (_l: number, _r: string) => true
// @ts-expect-error
useSelector(() => 1, compare2)
interface RootState {
property: string
}
const useTypedSelector: TypedUseSelectorHook<RootState> = useSelector
const r = useTypedSelector((state) => {
expectType<RootState>(state)
return state.property
})
expectType<string>(r)
}
function testUseStore() {
interface TypedState {
counter: number
active: boolean
}
interface TypedAction {
type: 'SET_STATE'
}
const untypedStore = useStore()
const state = untypedStore.getState()
expectType<unknown>(state)
const typedStore = useStore<TypedState, TypedAction>()
const typedState = typedStore.getState()
typedState.counter
// @ts-expect-error
typedState.things.stuff
}
// These should match the types of the hooks.
function testCreateHookFunctions() {
interface RootState {
property: string
}
interface RootAction {
type: 'TEST_ACTION'
}
const Context = React.createContext<ReactReduxContextValue<
RootState,
RootAction
> | null>(null)
// No context tests
expectType<() => Dispatch<AnyAction>>(createDispatchHook())
expectType<
<Selected extends unknown>(
selector: (state: any) => Selected,
equalityFn?: ((previous: Selected, next: Selected) => boolean) | undefined
) => Selected
>(createSelectorHook())
expectType<() => Store<any, AnyAction>>(createStoreHook())
// With context tests
expectType<() => Dispatch<RootAction>>(createDispatchHook(Context))
expectType<
<Selected extends unknown>(
selector: (state: RootState) => Selected,
equalityFn?: ((previous: Selected, next: Selected) => boolean) | undefined
) => Selected
>(createSelectorHook(Context))
expectType<() => Store<RootState, RootAction>>(createStoreHook(Context))
}