-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathextended-redux.ts
87 lines (71 loc) · 2.31 KB
/
extended-redux.ts
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
// This file tests behavior of types when we import the additional "extend-redux" module,
// which globally alters the Redux `bindActionCreators` and `Dispatch` types to assume
// that the thunk middleware always exists
import {
applyMiddleware,
bindActionCreators,
createStore,
Dispatch,
} from 'redux';
import thunk, {
ThunkAction,
ThunkActionDispatch,
ThunkDispatch,
ThunkMiddleware,
} from '../../src/index';
// MAGIC: Import a TS file that extends the `redux` module types
// This file must be kept separate from the primary typetest file to keep from
// polluting the type definitions over there
import '../../extend-redux';
export type State = {
foo: string;
};
export type Actions = { type: 'FOO' } | { type: 'BAR'; result: number };
export type ThunkResult<R> = ThunkAction<R, State, undefined, Actions>;
export const initialState: State = {
foo: 'foo',
};
export function fakeReducer(
state: State = initialState,
action: Actions,
): State {
return state;
}
export const store = createStore(
fakeReducer,
applyMiddleware(thunk as ThunkMiddleware<State, Actions>),
);
export function anotherThunkAction(): ThunkResult<string> {
return (dispatch, getState) => {
dispatch({ type: 'FOO' });
return 'hello';
};
}
function promiseThunkAction(): ThunkResult<Promise<boolean>> {
return async (dispatch, getState) => {
dispatch({ type: 'FOO' });
return false;
};
}
const standardAction = () => ({ type: 'FOO' });
interface ActionDispatchs {
anotherThunkAction: ThunkActionDispatch<typeof anotherThunkAction>;
promiseThunkAction: ThunkActionDispatch<typeof promiseThunkAction>;
standardAction: typeof standardAction;
}
// test that bindActionCreators correctly returns actions responses of ThunkActions
// also ensure standard action creators still work as expected.
// Unlike the main file, this declaration should compile okay because we've imported
// the global module override
const actions: ActionDispatchs = bindActionCreators(
{
anotherThunkAction,
promiseThunkAction,
standardAction,
},
store.dispatch,
);
const untypedStore = createStore(fakeReducer, applyMiddleware(thunk));
// Similarly, both of these declarations should pass okay as well
untypedStore.dispatch(anotherThunkAction());
untypedStore.dispatch(promiseThunkAction()).then(() => Promise.resolve());