diff --git a/index.d.ts b/index.d.ts index b91207af14..3a8413fad9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -75,10 +75,6 @@ export function combineReducers(reducers: ReducersMapObject): Reducer; /* store */ -export interface MiddlewareDispatch { - (action: TMiddlewareAction): TMiddlewareActionResult; -} - /** * A *dispatching function* (or simply *dispatch function*) is a function that * accepts an action or an async action; it then may or may not dispatch one @@ -97,8 +93,8 @@ export interface MiddlewareDispatch { * transform, delay, ignore, or otherwise interpret actions or async actions * before passing them to the next middleware. */ -export interface Dispatch extends MiddlewareDispatch { - (action: Action): Action; +export interface Dispatch { + (action: A): A; } /** @@ -142,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. @@ -257,7 +253,7 @@ export const createStore: StoreCreator; /* middleware */ export interface MiddlewareAPI { - dispatch: Dispatch; + dispatch: Dispatch; getState(): S; } @@ -271,7 +267,7 @@ export interface MiddlewareAPI { * asynchronous API call into a series of synchronous actions. */ export interface Middleware { - (api: MiddlewareAPI): (next: MiddlewareDispatch) => MiddlewareDispatch; + (api: MiddlewareAPI): (next: Dispatch) => Dispatch; } /** @@ -343,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 7cdfb2e254..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'}); +// thunk +declare module "../../index.d.ts" { + export interface Dispatch { + (asyncAction: (dispatch: Dispatch, getState: () => S) => R): R; + } +} -type Thunk = () => O; - -const dispatchThunkResult: number = dispatch, number>(() => 42); +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'}) }