Skip to content

Commit a30fe8f

Browse files
committed
chore(types/actions): allow defining types for multiple arguments
1 parent 72f5fc9 commit a30fe8f

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
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<A, P = any> = (...args: P[]) => 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: 3 additions & 3 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, P = any> {
53-
(...args: P[]): 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, P = any> {
59+
export interface ActionCreatorsMapObject<A = any, P extends any[] = any[]> {
6060
[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, string> = text => ({
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, string> = text => (
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, string>,
37-
ActionCreator<AddTodoAction, string>
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, string>
51+
addTodoViaThunk: ActionCreator<AddTodoThunk, [string]>
5252
}
5353

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

5858
const boundActionCreators2 = bindActionCreators<M, N>(

0 commit comments

Comments
 (0)