Skip to content

Commit 380e8b0

Browse files
authored
refactor(types/actions): allow type-setting args on ActionCreator (#3797)
1 parent 8551ba8 commit 380e8b0

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

docs/Glossary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ The base dispatch function _always_ synchronously sends an action to the store's
6666
## Action Creator
6767

6868
```js
69-
type ActionCreator = (...args: any) => Action | AsyncAction
69+
type ActionCreator<A, P extends any[] = any[]> = (...args: P) => Action | AsyncAction
7070
```
7171

7272
An _action creator_ is, quite simply, a function that creates an action. Do not confuse the two terms—again, an action is a payload of information, and an action creator is a factory that creates an action.

src/types/actions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ export interface AnyAction extends Action {
4949
*
5050
* @template A Returned action type.
5151
*/
52-
export interface ActionCreator<A> {
53-
(...args: any[]): A
52+
export interface ActionCreator<A, P extends any[] = any[]> {
53+
(...args: P): A
5454
}
5555

5656
/**
5757
* Object whose values are action creator functions.
5858
*/
59-
export interface ActionCreatorsMapObject<A = any> {
60-
[key: string]: ActionCreator<A>
59+
export interface ActionCreatorsMapObject<A = any, P extends any[] = any[]> {
60+
[key: string]: ActionCreator<A, P>
6161
}

test/typescript/actionCreators.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface AddTodoAction extends Action {
1010
text: string
1111
}
1212

13-
const addTodo: ActionCreator<AddTodoAction> = (text: string) => ({
13+
const addTodo: ActionCreator<AddTodoAction, [string]> = text => ({
1414
type: 'ADD_TODO',
1515
text
1616
})
@@ -19,7 +19,7 @@ const addTodoAction: AddTodoAction = addTodo('test')
1919

2020
type AddTodoThunk = (dispatch: Dispatch) => AddTodoAction
2121

22-
const addTodoViaThunk: ActionCreator<AddTodoThunk> = (text: string) => (
22+
const addTodoViaThunk: ActionCreator<AddTodoThunk> = text => (
2323
dispatch: Dispatch
2424
) => ({
2525
type: 'ADD_TODO',
@@ -33,8 +33,8 @@ const boundAddTodo = bindActionCreators(addTodo, dispatch)
3333
const dispatchedAddTodoAction: AddTodoAction = boundAddTodo('test')
3434

3535
const boundAddTodoViaThunk = bindActionCreators<
36-
ActionCreator<AddTodoThunk>,
37-
ActionCreator<AddTodoAction>
36+
ActionCreator<AddTodoThunk, [string]>,
37+
ActionCreator<AddTodoAction, [string]>
3838
>(addTodoViaThunk, dispatch)
3939

4040
const dispatchedAddTodoViaThunkAction: AddTodoAction = boundAddTodoViaThunk(
@@ -48,11 +48,11 @@ const otherDispatchedAddTodoAction: AddTodoAction = boundActionCreators.addTodo(
4848
)
4949

5050
interface M extends ActionCreatorsMapObject {
51-
addTodoViaThunk: ActionCreator<AddTodoThunk>
51+
addTodoViaThunk: ActionCreator<AddTodoThunk, [string]>
5252
}
5353

5454
interface N extends ActionCreatorsMapObject {
55-
addTodoViaThunk: ActionCreator<AddTodoAction>
55+
addTodoViaThunk: ActionCreator<AddTodoAction, [string]>
5656
}
5757

5858
const boundActionCreators2 = bindActionCreators<M, N>(

0 commit comments

Comments
 (0)