Skip to content

Commit

Permalink
Fix TS definitions test for new Dispatch typing (#2674)
Browse files Browse the repository at this point in the history
* Fix TypeScript definitions test for new Dispatch typing

* Be explicit about type parameters
  • Loading branch information
pelotom authored and timdorr committed Oct 23, 2017
1 parent 80a5ac5 commit a2200d5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions test/typescript/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const dispatchResult: Action = dispatch({type: 'TYPE'});

// thunk
declare module "../../" {
export interface Dispatch<S> {
<R>(asyncAction: (dispatch: Dispatch<S>, getState: () => S) => R): R;
export interface Dispatch<D = Action> {
<R>(asyncAction: (dispatch: Dispatch<D>, getState: () => any) => R): R;
}
}

Expand Down
16 changes: 8 additions & 8 deletions test/typescript/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import {
} from "../../"

declare module "../../" {
export interface Dispatch<S> {
<R>(asyncAction: (dispatch: Dispatch<S>, getState: () => S) => R): R;
export interface Dispatch<D = Action> {
<R>(asyncAction: (dispatch: Dispatch<D>, getState: () => any) => R): R;
}
}

type Thunk<S, O> = (dispatch: Dispatch<S>, getState?: () => S) => O;
type Thunk<S, O> = (dispatch: Dispatch, getState?: () => S) => O;

const thunkMiddleware: Middleware =
<S>({dispatch, getState}: MiddlewareAPI<S>) =>
(next: Dispatch<S>) =>
<A extends Action, B>(action: A | Thunk<S, B>): B|Action =>
<S, A extends Action>({dispatch, getState}: MiddlewareAPI<S>) =>
(next: Dispatch<A>) =>
<B>(action: A | Thunk<S, B>): B|Action =>
typeof action === 'function' ?
(<Thunk<S, B>>action)(dispatch, getState) :
next(<A>action)


const loggerMiddleware: Middleware =
<S>({getState}: MiddlewareAPI<S>) =>
(next: Dispatch<S>) =>
(next: Dispatch) =>
(action: any): any => {
console.log('will dispatch', action)

Expand Down Expand Up @@ -51,7 +51,7 @@ const storeWithThunkMiddleware = createStore(
);

storeWithThunkMiddleware.dispatch(
(dispatch: Dispatch<State>, getState: () => State) => {
(dispatch: Dispatch, getState: () => State) => {
const todos: string[] = getState().todos;
dispatch({type: 'ADD_TODO'})
}
Expand Down

0 comments on commit a2200d5

Please sign in to comment.