|
1 | | -import type { ConfigureStoreOptions, PayloadAction } from '@reduxjs/toolkit' |
2 | | -import { Tuple, configureStore, createSlice } from '@reduxjs/toolkit' |
3 | 1 | import type { |
4 | 2 | Action, |
| 3 | + ConfigureStoreOptions, |
5 | 4 | Dispatch, |
6 | 5 | Middleware, |
| 6 | + PayloadAction, |
7 | 7 | Reducer, |
8 | 8 | Store, |
9 | 9 | StoreEnhancer, |
| 10 | + ThunkAction, |
| 11 | + ThunkDispatch, |
| 12 | + ThunkMiddleware, |
10 | 13 | UnknownAction, |
11 | | -} from 'redux' |
12 | | -import { applyMiddleware, combineReducers } from 'redux' |
13 | | -import type { ThunkAction, ThunkDispatch, ThunkMiddleware } from 'redux-thunk' |
| 14 | +} from '@reduxjs/toolkit' |
| 15 | +import { |
| 16 | + Tuple, |
| 17 | + applyMiddleware, |
| 18 | + combineReducers, |
| 19 | + configureStore, |
| 20 | + createSlice, |
| 21 | +} from '@reduxjs/toolkit' |
14 | 22 | import { thunk } from 'redux-thunk' |
15 | 23 |
|
16 | 24 | const _anyMiddleware: any = () => () => () => {} |
@@ -40,11 +48,12 @@ describe('type tests', () => { |
40 | 48 |
|
41 | 49 | test('configureStore() infers the store state type.', () => { |
42 | 50 | const reducer: Reducer<number> = () => 0 |
| 51 | + |
43 | 52 | const store = configureStore({ reducer }) |
44 | | - const numberStore: Store<number, UnknownAction> = store |
45 | 53 |
|
46 | | - // @ts-expect-error |
47 | | - const stringStore: Store<string, UnknownAction> = store |
| 54 | + expectTypeOf(store).toMatchTypeOf<Store<number, UnknownAction>>() |
| 55 | + |
| 56 | + expectTypeOf(store).not.toMatchTypeOf<Store<string, UnknownAction>>() |
48 | 57 | }) |
49 | 58 |
|
50 | 59 | test('configureStore() infers the store action type.', () => { |
@@ -402,7 +411,7 @@ describe('type tests', () => { |
402 | 411 | test('Dispatch typings', () => { |
403 | 412 | type StateA = number |
404 | 413 | const reducerA = () => 0 |
405 | | - function thunkA() { |
| 414 | + const thunkA = () => { |
406 | 415 | return (() => {}) as any as ThunkAction<Promise<'A'>, StateA, any, any> |
407 | 416 | } |
408 | 417 |
|
@@ -599,12 +608,6 @@ describe('type tests', () => { |
599 | 608 | > |
600 | 609 | >() |
601 | 610 |
|
602 | | - expectTypeOf(concatenated).not.toEqualTypeOf< |
603 | | - ReadonlyArray< |
604 | | - typeof otherMiddleware | ThunkMiddleware | Middleware<{}> |
605 | | - > |
606 | | - >() |
607 | | - |
608 | 611 | return concatenated |
609 | 612 | }, |
610 | 613 | }) |
@@ -720,57 +723,55 @@ describe('type tests', () => { |
720 | 723 | expectTypeOf(store.dispatch).toBeFunction() |
721 | 724 | }) |
722 | 725 |
|
723 | | - { |
724 | | - interface CounterState { |
725 | | - value: number |
726 | | - } |
| 726 | + interface CounterState { |
| 727 | + value: number |
| 728 | + } |
727 | 729 |
|
728 | | - const counterSlice = createSlice({ |
729 | | - name: 'counter', |
730 | | - initialState: { value: 0 } as CounterState, |
731 | | - reducers: { |
732 | | - increment(state) { |
733 | | - state.value += 1 |
734 | | - }, |
735 | | - decrement(state) { |
736 | | - state.value -= 1 |
737 | | - }, |
738 | | - // Use the PayloadAction type to declare the contents of `action.payload` |
739 | | - incrementByAmount: (state, action: PayloadAction<number>) => { |
740 | | - state.value += action.payload |
741 | | - }, |
| 730 | + const counterSlice = createSlice({ |
| 731 | + name: 'counter', |
| 732 | + initialState: { value: 0 } as CounterState, |
| 733 | + reducers: { |
| 734 | + increment(state) { |
| 735 | + state.value += 1 |
742 | 736 | }, |
743 | | - }) |
| 737 | + decrement(state) { |
| 738 | + state.value -= 1 |
| 739 | + }, |
| 740 | + // Use the PayloadAction type to declare the contents of `action.payload` |
| 741 | + incrementByAmount: (state, action: PayloadAction<number>) => { |
| 742 | + state.value += action.payload |
| 743 | + }, |
| 744 | + }, |
| 745 | + }) |
744 | 746 |
|
745 | | - type Unsubscribe = () => void |
| 747 | + type Unsubscribe = () => void |
746 | 748 |
|
747 | | - // A fake middleware that tells TS that an unsubscribe callback is being returned for a given action |
748 | | - // This is the same signature that the "listener" middleware uses |
749 | | - const dummyMiddleware: Middleware< |
750 | | - { |
751 | | - (action: Action<'actionListenerMiddleware/add'>): Unsubscribe |
752 | | - }, |
753 | | - CounterState |
754 | | - > = (storeApi) => (next) => (action) => {} |
| 749 | + // A fake middleware that tells TS that an unsubscribe callback is being returned for a given action |
| 750 | + // This is the same signature that the "listener" middleware uses |
| 751 | + const dummyMiddleware: Middleware< |
| 752 | + { |
| 753 | + (action: Action<'actionListenerMiddleware/add'>): Unsubscribe |
| 754 | + }, |
| 755 | + CounterState |
| 756 | + > = (storeApi) => (next) => (action) => {} |
755 | 757 |
|
756 | | - const store = configureStore({ |
757 | | - reducer: counterSlice.reducer, |
758 | | - middleware: (gDM) => gDM().prepend(dummyMiddleware), |
759 | | - }) |
| 758 | + const store = configureStore({ |
| 759 | + reducer: counterSlice.reducer, |
| 760 | + middleware: (gDM) => gDM().prepend(dummyMiddleware), |
| 761 | + }) |
760 | 762 |
|
761 | | - // Order matters here! We need the listener type to come first, otherwise |
762 | | - // the thunk middleware type kicks in and TS thinks a plain action is being returned |
763 | | - expectTypeOf(store.dispatch).toEqualTypeOf< |
764 | | - ((action: Action<'actionListenerMiddleware/add'>) => Unsubscribe) & |
765 | | - ThunkDispatch<CounterState, undefined, UnknownAction> & |
766 | | - Dispatch<UnknownAction> |
767 | | - >() |
| 763 | + // Order matters here! We need the listener type to come first, otherwise |
| 764 | + // the thunk middleware type kicks in and TS thinks a plain action is being returned |
| 765 | + expectTypeOf(store.dispatch).toEqualTypeOf< |
| 766 | + ((action: Action<'actionListenerMiddleware/add'>) => Unsubscribe) & |
| 767 | + ThunkDispatch<CounterState, undefined, UnknownAction> & |
| 768 | + Dispatch<UnknownAction> |
| 769 | + >() |
768 | 770 |
|
769 | | - const unsubscribe = store.dispatch({ |
770 | | - type: 'actionListenerMiddleware/add', |
771 | | - } as const) |
| 771 | + const unsubscribe = store.dispatch({ |
| 772 | + type: 'actionListenerMiddleware/add', |
| 773 | + } as const) |
772 | 774 |
|
773 | | - expectTypeOf(unsubscribe).toEqualTypeOf<Unsubscribe>() |
774 | | - } |
| 775 | + expectTypeOf(unsubscribe).toEqualTypeOf<Unsubscribe>() |
775 | 776 | }) |
776 | 777 | }) |
0 commit comments