Skip to content

Commit b0831d2

Browse files
committed
Add type tests for withTypes
1 parent df1ad38 commit b0831d2

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

test/typetests/hooks.tsx

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,12 @@ import {
2121
import type { AppDispatch, AppStore, RootState } from './counterApp'
2222
import { incrementAsync } from './counterApp'
2323

24-
import { expectExactType, expectType } from '../typeTestHelpers'
24+
import { exactType, expectExactType, expectType } from '../typeTestHelpers'
2525

2626
function preTypedHooksSetup() {
2727
// 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
3530

3631
function CounterComponent() {
3732
const dispatch = useAppDispatch()
@@ -242,3 +237,43 @@ function testCreateHookFunctions() {
242237
>(createSelectorHook(Context))
243238
expectType<() => Store<RootState, RootAction>>(createStoreHook(Context))
244239
}
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

Comments
 (0)