Skip to content

refactor(types/actions): allow type-setting args on ActionCreator #3797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ The base dispatch function _always_ synchronously sends an action to the store's
## Action Creator

```js
type ActionCreator = (...args: any) => Action | AsyncAction
type ActionCreator<A, P extends any[] = any[]> = (...args: P) => Action | AsyncAction
```

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.
Expand Down
8 changes: 4 additions & 4 deletions src/types/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ export interface AnyAction extends Action {
*
* @template A Returned action type.
*/
export interface ActionCreator<A> {
(...args: any[]): A
export interface ActionCreator<A, P extends any[] = any[]> {
(...args: P): A
}

/**
* Object whose values are action creator functions.
*/
export interface ActionCreatorsMapObject<A = any> {
[key: string]: ActionCreator<A>
export interface ActionCreatorsMapObject<A = any, P extends any[] = any[]> {
[key: string]: ActionCreator<A, P>
}
12 changes: 6 additions & 6 deletions test/typescript/actionCreators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface AddTodoAction extends Action {
text: string
}

const addTodo: ActionCreator<AddTodoAction> = (text: string) => ({
const addTodo: ActionCreator<AddTodoAction, [string]> = text => ({
type: 'ADD_TODO',
text
})
Expand All @@ -19,7 +19,7 @@ const addTodoAction: AddTodoAction = addTodo('test')

type AddTodoThunk = (dispatch: Dispatch) => AddTodoAction

const addTodoViaThunk: ActionCreator<AddTodoThunk> = (text: string) => (
const addTodoViaThunk: ActionCreator<AddTodoThunk> = text => (
dispatch: Dispatch
) => ({
type: 'ADD_TODO',
Expand All @@ -33,8 +33,8 @@ const boundAddTodo = bindActionCreators(addTodo, dispatch)
const dispatchedAddTodoAction: AddTodoAction = boundAddTodo('test')

const boundAddTodoViaThunk = bindActionCreators<
ActionCreator<AddTodoThunk>,
ActionCreator<AddTodoAction>
ActionCreator<AddTodoThunk, [string]>,
ActionCreator<AddTodoAction, [string]>
>(addTodoViaThunk, dispatch)

const dispatchedAddTodoViaThunkAction: AddTodoAction = boundAddTodoViaThunk(
Expand All @@ -48,11 +48,11 @@ const otherDispatchedAddTodoAction: AddTodoAction = boundActionCreators.addTodo(
)

interface M extends ActionCreatorsMapObject {
addTodoViaThunk: ActionCreator<AddTodoThunk>
addTodoViaThunk: ActionCreator<AddTodoThunk, [string]>
}

interface N extends ActionCreatorsMapObject {
addTodoViaThunk: ActionCreator<AddTodoAction>
addTodoViaThunk: ActionCreator<AddTodoAction, [string]>
}

const boundActionCreators2 = bindActionCreators<M, N>(
Expand Down