-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathtypescript.ts
166 lines (143 loc) · 4.21 KB
/
typescript.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
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
/* eslint-disable @typescript-eslint/no-unused-vars */
import {
applyMiddleware,
bindActionCreators,
createStore,
Action,
AnyAction
} from 'redux'
import thunk, {
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<R, State, undefined, Actions>
export const initialState: State = {
foo: 'foo'
}
export function fakeReducer(state: State = initialState): 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 })
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' })
store.dispatch({ type: 'BAR' })
store.dispatch({ type: 'BAR', result: 5 })
store.dispatch({ type: 'BAZ' })
store.dispatch(testGetState())
const storeThunkArg = createStore(
fakeReducer,
applyMiddleware(
thunk.withExtraArgument('bar') as ThunkMiddleware<State, Actions, string>
)
)
storeThunkArg.dispatch({ type: 'FOO' })
storeThunkArg.dispatch((dispatch, getState, extraArg) => {
const bar: string = extraArg
store.dispatch({ type: 'FOO' })
store.dispatch({ type: 'BAR' })
store.dispatch({ type: 'BAR', result: 5 })
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))
// @ts-expect-error
untypedStore.dispatch(anotherThunkAction())
// @ts-expect-error
untypedStore.dispatch(promiseThunkAction()).then(() => Promise.resolve())
// #248: Need a union overload to handle generic dispatched types
function testIssue248() {
const dispatch: ThunkDispatch<any, unknown, AnyAction> = undefined as any
function dispatchWrap(
action: Action | ThunkAction<any, any, unknown, AnyAction>
) {
// Should not have an error here thanks to the extra union overload
dispatch(action)
}
}