From f482096dc54638fb2211d673d762f3d051fa271e Mon Sep 17 00:00:00 2001 From: Boris Sergeyev Date: Sat, 3 Nov 2018 19:20:07 +0100 Subject: [PATCH] [react-redux] implemented proper typings for thunk action creators in TDispatchProps --- types/react-redux/index.d.ts | 38 +++++++++++++++++++++---- types/react-redux/react-redux-tests.tsx | 23 +++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/types/react-redux/index.d.ts b/types/react-redux/index.d.ts index 5cbde2e8db..0676941ce9 100644 --- a/types/react-redux/index.d.ts +++ b/types/react-redux/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for react-redux 6.0.4 +// Type definitions for react-redux 6.0.5 // Project: https://github.com/reduxjs/react-redux // Definitions by: Qubo , // Kenzie Togami , @@ -11,6 +11,7 @@ // Valentin Descamps // Johann Rakotoharisoa // Anatoli Papirovski +// Boris Sergeyev // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 @@ -102,7 +103,7 @@ type ConnectedComponentClass = ComponentClass { - >>>( + >>>( component: C ): ConnectedComponentClass, keyof Shared>> & TNeedsProps> } @@ -113,6 +114,19 @@ export interface InferableComponentEnhancerWithProps = InferableComponentEnhancerWithProps +// redux-thunk middleware returns thunk's return value from dispatch call +type WithThunkActionCreators = + TDispatchProps extends object + ? { + [C in keyof TDispatchProps]: C extends (...args: any[]) => any + ? ReturnType extends (...args: any[]) => any + ? ReturnType> + : C + : never + } + : TDispatchProps + + /** * Connects a React component to a Redux store. * @@ -142,12 +156,18 @@ export interface Connect { ( mapStateToProps: null | undefined, mapDispatchToProps: MapDispatchToPropsParam - ): InferableComponentEnhancerWithProps; + ): InferableComponentEnhancerWithProps< + WithThunkActionCreators, + TOwnProps + >; ( mapStateToProps: MapStateToPropsParam, mapDispatchToProps: MapDispatchToPropsParam - ): InferableComponentEnhancerWithProps; + ): InferableComponentEnhancerWithProps< + TStateProps & WithThunkActionCreators, + TOwnProps + >; ( mapStateToProps: MapStateToPropsParam, @@ -185,14 +205,20 @@ export interface Connect { mapDispatchToProps: MapDispatchToPropsParam, mergeProps: null | undefined, options: Options<{}, TStateProps, TOwnProps> - ): InferableComponentEnhancerWithProps; + ): InferableComponentEnhancerWithProps< + WithThunkActionCreators, + TOwnProps + >; ( mapStateToProps: MapStateToPropsParam, mapDispatchToProps: MapDispatchToPropsParam, mergeProps: null | undefined, options: Options - ): InferableComponentEnhancerWithProps; + ): InferableComponentEnhancerWithProps< + TStateProps & WithThunkActionCreators, + TOwnProps + >; ( mapStateToProps: MapStateToPropsParam, diff --git a/types/react-redux/react-redux-tests.tsx b/types/react-redux/react-redux-tests.tsx index 3b25b25843..a5f8c2b4f7 100644 --- a/types/react-redux/react-redux-tests.tsx +++ b/types/react-redux/react-redux-tests.tsx @@ -99,6 +99,29 @@ namespace MapDispatch { const verifyUndefined = } +namespace MapDispatchWithThunkActionCreators { + interface OwnProps { foo: string } + interface DispatchProps { + onClick(): void, + thunkAction(): Promise + } + + class TestComponent extends Component {} + + const mapDispatchToProps = () => ({ + onClick: () => {}, + thunkAction: () => async () => {} + }) + + const Test = connect( + null, + mapDispatchToProps, + )(TestComponent) + + const verify = +} + + namespace MapStateAndDispatchObject { interface ClickPayload { count: number } const onClick: ActionCreator = () => ({ count: 1 });