-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathtypescript.ts
More file actions
187 lines (161 loc) · 4.74 KB
/
Copy pathtypescript.ts
File metadata and controls
187 lines (161 loc) · 4.74 KB
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
/* eslint-disable @typescript-eslint/no-unused-vars */
import { applyMiddleware, bindActionCreators, createStore } from 'redux'
import type { Action, AnyAction } from 'redux'
import { thunk, withExtraArgument } from '../src/index'
import type {
ThunkAction,
ThunkActionDispatch,
ThunkDispatch,
ThunkMiddleware
} from '../src/index'
export type State = {
foo: string
}
export type Actions = { type: 'FOO' } | { type: 'BAR'; result: number }
export type ThunkResult<R> = ThunkAction<
ThunkDispatch<State, undefined, Actions>,
State,
undefined,
R
>
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>)
)
store.dispatch(dispatch => {
dispatch({ type: 'FOO' })
// @ts-expect-error
dispatch({ type: 'BAR' }, 42)
dispatch({ type: 'BAR', result: 5 })
// @ts-expect-error
store.dispatch({ type: 'BAZ' })
})
function testGetState(): ThunkResult<void> {
return (dispatch, getState) => {
const state = getState()
const { foo } = state
dispatch({ type: 'FOO' })
// @ts-expect-error
dispatch({ type: 'BAR' })
dispatch({ type: 'BAR', result: 5 })
// @ts-expect-error
dispatch({ type: 'BAZ' })
// Can dispatch another thunk action
dispatch(anotherThunkAction())
}
}
export function anotherThunkAction(): ThunkResult<string> {
return (dispatch, getState) => {
dispatch({ type: 'FOO' })
return 'hello'
}
}
store.dispatch({ type: 'FOO' })
// @ts-expect-error
store.dispatch({ type: 'BAR' })
store.dispatch({ type: 'BAR', result: 5 })
// @ts-expect-error
store.dispatch({ type: 'BAZ' })
store.dispatch(testGetState())
const storeThunkArg = createStore(
fakeReducer,
applyMiddleware(
withExtraArgument('bar') as ThunkMiddleware<State, Actions, string>
)
)
storeThunkArg.dispatch({ type: 'FOO' })
storeThunkArg.dispatch((dispatch, getState, extraArg) => {
const bar: string = extraArg
store.dispatch({ type: 'FOO' })
// @ts-expect-error
store.dispatch({ type: 'BAR' })
store.dispatch({ type: 'BAR', result: 5 })
// @ts-expect-error
store.dispatch({ type: 'BAZ' })
console.log(extraArg)
})
const callDispatchAsync_anyAction = (
dispatch: ThunkDispatch<State, undefined, any>
) => {
const asyncThunk = (): ThunkResult<Promise<void>> => () =>
({} as Promise<void>)
dispatch(asyncThunk()).then(() => console.log('done'))
}
const callDispatchAsync_specificActions = (
dispatch: ThunkDispatch<State, undefined, Actions>
) => {
const asyncThunk = (): ThunkResult<Promise<void>> => () =>
({} as Promise<void>)
dispatch(asyncThunk()).then(() => console.log('done'))
}
const callDispatchAny = (
dispatch: ThunkDispatch<State, undefined, Actions>
) => {
const asyncThunk = (): any => () => ({} as Promise<void>)
dispatch(asyncThunk()) // result is any
.then(() => console.log('done'))
}
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
}
// Without a global module overload, this should fail
// @ts-expect-error
const actions: ActionDispatchs = bindActionCreators(
{
anotherThunkAction,
promiseThunkAction,
standardAction
},
store.dispatch
)
actions.anotherThunkAction() === 'hello'
// @ts-expect-error
actions.anotherThunkAction() === false
actions.promiseThunkAction().then(res => console.log(res))
// @ts-expect-error
actions.promiseThunkAction().prop
actions.standardAction().type
// @ts-expect-error
actions.standardAction().other
const untypedStore = createStore(fakeReducer, applyMiddleware(thunk))
untypedStore.dispatch(anotherThunkAction())
untypedStore.dispatch(promiseThunkAction()).then(() => Promise.resolve())
// #248: Need a union overload to handle generic dispatched types
function testIssue248() {
function dispatchWrap(
action: Actions | ThunkAction<any, any, unknown, Actions>
) {
// this errors, because the union overload is not present
// @ts-expect-error
store.dispatch(action)
// workarounds:
// assign to ThunkDispatch type
// Should not have an error here thanks to the extra union overload
const dispatch: ThunkDispatch<any, unknown, Actions> = store.dispatch
dispatch(action)
// old reliable
store.dispatch(action as any)
// non-ideal, but works
typeof action === 'function'
? store.dispatch(action)
: store.dispatch(action)
}
}