Skip to content
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

Make typings compatible with Redux 4.0.0 #180

Merged
merged 10 commits into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
24 changes: 15 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import {Middleware, Dispatch} from "redux";
import { Middleware, Action, AnyAction } from "redux";


export type ThunkAction<R, S, E> = (dispatch: Dispatch<S>, getState: () => S,
extraArgument: E) => R;
export type ThunkAction<R, S, E, A extends Action> = (
dispatch: ThunkDispatch<S, E, A>,
getState: () => S,
extraArgument: E
) => R;

declare module "redux" {
export interface Dispatch<S> {
<R, E>(asyncAction: ThunkAction<R, S, E>): R;
}
interface ThunkDispatch<S, E, A extends Action> {
<T extends A>(action: T): T;
}

interface ThunkDispatch<S, E, A extends Action> {
<R>(asyncAction: ThunkAction<R, S, E, A>): R;
}

type ThunkMiddleware<E, S = {}, A extends Action = AnyAction> = Middleware<ThunkDispatch<S, E, A>, S, ThunkDispatch<S, E, A>>;
Copy link
Contributor Author

@Cryrivers Cryrivers Feb 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E could be inferred in withExtraArgument. S could be inferred in Middleware. Now the problem is A is not inferable. I'm thinking of opening a PR in https://github.com/reactjs/redux to pass action types to Middleware

EDIT: I'm wrong I guess.


declare const thunk: Middleware & {
withExtraArgument(extraArgument: any): Middleware;
declare const thunk: ThunkMiddleware<undefined> & {
withExtraArgument<E>(extraArgument: E): ThunkMiddleware<E>
};

export default thunk;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@
"eslint-config-airbnb": "1.0.2",
"eslint-plugin-react": "^4.1.0",
"mocha": "^2.2.5",
"redux": "^3.4.0",
"redux": "4.0.0-beta.2",
"rimraf": "^2.5.2",
"typescript": "^1.8.10",
"typescript-definition-tester": "0.0.4",
"typescript": "~2.4.0",
"typescript-definition-tester": "0.0.5",
"webpack": "^1.12.14"
}
}
55 changes: 37 additions & 18 deletions test/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
import {Store, Middleware} from 'redux';
import thunk, {ThunkAction} from '../index.d.ts';
import { createStore, applyMiddleware } from 'redux';
import thunk, { ThunkAction } from '../index';

type State = {
foo: string;
};

type Actions = { type: 'FOO' };

declare const store: Store<{foo: string}>;
function fakeReducer(state: State, action: Actions): State {
return state;
}

const store = createStore(fakeReducer, applyMiddleware(thunk));

store.dispatch(dispatch => {
dispatch({type: 'FOO'});
dispatch({ type: 'FOO' });
});

store.dispatch((dispatch, getState) => {
const state = getState();
function testGetState(): ThunkAction<void, State, {}, Actions> {
return (dispatch, getState) => {
const state = getState();
const foo: string = state.foo;
};
}

const foo: string = state.foo;
});
store.dispatch(testGetState());

const middleware: Middleware = thunk.withExtraArgument('bar');
const storeThunkArg = createStore(
fakeReducer,
applyMiddleware(thunk.withExtraArgument('bar'))
);

store.dispatch((dispatch, getState, extraArg) => {
storeThunkArg.dispatch((dispatch, getState, extraArg) => {
const bar: string = extraArg;
console.log(extraArg);
});

const thunkAction: ThunkAction<void, {foo: string}, {bar: number}> =
(dispatch, getState, extraArg) => {
const foo: string = getState().foo;
const bar: number = extraArg.bar;
const thunkAction: ThunkAction<void, State, { bar: number }, Actions> = (
dispatch,
getState,
extraArg
) => {
const foo: string = getState().foo;
const bar: number = extraArg.bar;

dispatch({type: 'FOO'});
};
dispatch({ type: 'FOO' });
};

const thunkActionDispatchOnly: ThunkAction<void, {}, {}> = dispatch => {
dispatch({type: 'FOO'});
const thunkActionDispatchOnly: ThunkAction<void, {}, {}, Actions> = dispatch => {
dispatch({ type: 'FOO' });
};