@@ -21,17 +21,12 @@ import {
21
21
import type { AppDispatch , AppStore , RootState } from './counterApp'
22
22
import { incrementAsync } from './counterApp'
23
23
24
- import { expectExactType , expectType } from '../typeTestHelpers'
24
+ import { exactType , expectExactType , expectType } from '../typeTestHelpers'
25
25
26
26
function preTypedHooksSetup ( ) {
27
27
// Standard hooks setup
28
- const useAppDispatch = useDispatch . withTypes < AppDispatch > ( )
29
- // const useAppDispatch = () => useDispatch<AppDispatch>()
30
- // const useAppSelector: UseSelector<RootState> = useSelector
31
- const useAppSelector = useSelector . withTypes < RootState > ( )
32
-
33
- useAppSelector ( ( state ) => state . counter )
34
- const useAppStore = useStore . withTypes < AppStore > ( )
28
+ const useAppDispatch = ( ) => useDispatch < AppDispatch > ( )
29
+ const useAppSelector : TypedUseSelectorHook < RootState > = useSelector
35
30
36
31
function CounterComponent ( ) {
37
32
const dispatch = useAppDispatch ( )
@@ -242,3 +237,43 @@ function testCreateHookFunctions() {
242
237
> ( createSelectorHook ( Context ) )
243
238
expectType < ( ) => Store < RootState , RootAction > > ( createStoreHook ( Context ) )
244
239
}
240
+
241
+ function preTypedHooksSetupWithTypes ( ) {
242
+ const useAppDispatch = useDispatch . withTypes < AppDispatch > ( )
243
+
244
+ const useAppSelector = useSelector . withTypes < RootState > ( )
245
+
246
+ const useAppStore = useStore . withTypes < AppStore > ( )
247
+
248
+ function CounterComponent ( ) {
249
+ useAppSelector ( ( state ) => state . counter )
250
+
251
+ const dispatch = useAppDispatch ( )
252
+
253
+ expectExactType < AppDispatch > ( dispatch )
254
+
255
+ const store = useAppStore ( )
256
+
257
+ expectExactType < AppStore > ( store )
258
+
259
+ expectExactType < AppDispatch > ( store . dispatch )
260
+
261
+ const state = store . getState ( )
262
+
263
+ expectExactType < RootState > ( state )
264
+
265
+ expectExactType < number > ( state . counter )
266
+
267
+ store . dispatch ( incrementAsync ( 1 ) )
268
+
269
+ exactType ( store . dispatch , dispatch )
270
+
271
+ return (
272
+ < button
273
+ onClick = { ( ) => {
274
+ dispatch ( incrementAsync ( 1 ) )
275
+ } }
276
+ />
277
+ )
278
+ }
279
+ }
0 commit comments