diff --git a/index.d.ts b/index.d.ts index ad6973eb71..3a8413fad9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -93,7 +93,9 @@ export function combineReducers(reducers: ReducersMapObject): Reducer; * transform, delay, ignore, or otherwise interpret actions or async actions * before passing them to the next middleware. */ -export type Dispatch = (action: any) => any; +export interface Dispatch { + (action: A): A; +} /** * Function to remove listener added by `Store.subscribe()`. @@ -136,7 +138,7 @@ export interface Store { * Note that, if you use a custom middleware, it may wrap `dispatch()` to * return something else (for example, a Promise you can await). */ - dispatch: Dispatch; + dispatch: Dispatch; /** * Reads the state tree managed by the store. @@ -251,7 +253,7 @@ export const createStore: StoreCreator; /* middleware */ export interface MiddlewareAPI { - dispatch: Dispatch; + dispatch: Dispatch; getState(): S; } @@ -265,7 +267,7 @@ export interface MiddlewareAPI { * asynchronous API call into a series of synchronous actions. */ export interface Middleware { - (api: MiddlewareAPI): (next: Dispatch) => (action: any) => any; + (api: MiddlewareAPI): (next: Dispatch) => Dispatch; } /** @@ -337,19 +339,19 @@ export interface ActionCreatorsMapObject { * creator wrapped into the `dispatch` call. If you passed a function as * `actionCreator`, the return value will also be a single function. */ -export function bindActionCreators>(actionCreator: A, dispatch: Dispatch): A; +export function bindActionCreators>(actionCreator: A, dispatch: Dispatch): A; export function bindActionCreators< A extends ActionCreator, B extends ActionCreator - >(actionCreator: A, dispatch: Dispatch): B; + >(actionCreator: A, dispatch: Dispatch): B; -export function bindActionCreators(actionCreators: M, dispatch: Dispatch): M; +export function bindActionCreators(actionCreators: M, dispatch: Dispatch): M; export function bindActionCreators< M extends ActionCreatorsMapObject, N extends ActionCreatorsMapObject - >(actionCreators: M, dispatch: Dispatch): N; + >(actionCreators: M, dispatch: Dispatch): N; /* compose */ diff --git a/test/typescript/actionCreators.ts b/test/typescript/actionCreators.ts index 77daa9d739..c3eac83b00 100644 --- a/test/typescript/actionCreators.ts +++ b/test/typescript/actionCreators.ts @@ -15,15 +15,15 @@ const addTodo: ActionCreator = (text: string) => ({ const addTodoAction: AddTodoAction = addTodo('test'); -type AddTodoThunk = (dispatch: Dispatch) => AddTodoAction; +type AddTodoThunk = (dispatch: Dispatch) => AddTodoAction; const addTodoViaThunk: ActionCreator = (text: string) => - (dispatch: Dispatch) => ({ + (dispatch: Dispatch) => ({ type: 'ADD_TODO', text }) -declare const dispatch: Dispatch; +declare const dispatch: Dispatch; const boundAddTodo = bindActionCreators(addTodo, dispatch); diff --git a/test/typescript/dispatch.ts b/test/typescript/dispatch.ts index b589592f1b..bc54b17fb9 100644 --- a/test/typescript/dispatch.ts +++ b/test/typescript/dispatch.ts @@ -1,12 +1,16 @@ import {Dispatch, Action} from "../../index.d.ts"; -declare const dispatch: Dispatch; - +declare const dispatch: Dispatch; const dispatchResult: Action = dispatch({type: 'TYPE'}); - -type Thunk = () => O; +// thunk +declare module "../../index.d.ts" { + export interface Dispatch { + (asyncAction: (dispatch: Dispatch, getState: () => S) => R): R; + } +} const dispatchThunkResult: number = dispatch(() => 42); +const dispatchedTimerId: number = dispatch(d => setTimeout(() => d({type: 'TYPE'}), 1000)); diff --git a/test/typescript/middleware.ts b/test/typescript/middleware.ts index d5738dc844..5111873df9 100644 --- a/test/typescript/middleware.ts +++ b/test/typescript/middleware.ts @@ -3,21 +3,26 @@ import { applyMiddleware, createStore, Dispatch, Reducer, Action } from "../../index.d.ts"; +declare module "../../index.d.ts" { + export interface Dispatch { + (asyncAction: (dispatch: Dispatch, getState: () => S) => R): R; + } +} -type Thunk = (dispatch: Dispatch, getState?: () => S) => O; +type Thunk = (dispatch: Dispatch, getState: () => S) => O; const thunkMiddleware: Middleware = ({dispatch, getState}: MiddlewareAPI) => - (next: Dispatch) => - (action: A | Thunk): B => + (next: Dispatch) => + (action: A | Thunk): B|Action => typeof action === 'function' ? (>action)(dispatch, getState) : - next(action) + next(action) const loggerMiddleware: Middleware = ({getState}: MiddlewareAPI) => - (next: Dispatch) => + (next: Dispatch) => (action: any): any => { console.log('will dispatch', action) @@ -47,7 +52,7 @@ const storeWithThunkMiddleware = createStore( ); storeWithThunkMiddleware.dispatch( - (dispatch: Dispatch, getState: () => State) => { + (dispatch, getState) => { const todos: string[] = getState().todos; dispatch({type: 'ADD_TODO'}) }