From b7486f2c4df8217e9e8ab8a95d1152c55a401b17 Mon Sep 17 00:00:00 2001 From: Marco Buono Date: Fri, 18 Aug 2017 21:06:54 -0300 Subject: [PATCH] react-redux: fix dispatchToProps as object This fixes the resulting wrapped component type returned by the connect HOC when the `dispatchToProps` argument is an object. (Fixes issue #18955) Previously we were using `MapDispatchToPropsObject` but that doesn't work with `Omit`, so dispatch props were being preserved on the resulting object. I've replaced that with `TDispatchProps` instead. I've added test code by @horiuchi, adapted from: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18955 --- types/react-redux/index.d.ts | 10 +++------- types/react-redux/react-redux-tests.tsx | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/types/react-redux/index.d.ts b/types/react-redux/index.d.ts index 98f649e9f8..32e8919075 100644 --- a/types/react-redux/index.d.ts +++ b/types/react-redux/index.d.ts @@ -148,12 +148,8 @@ interface MapDispatchToPropsFunction { (dispatch: Dispatch, ownProps: TOwnProps): TDispatchProps; } -interface MapDispatchToPropsObject { - [name: string]: ActionCreator; -} - type MapDispatchToProps = - MapDispatchToPropsFunction | MapDispatchToPropsObject; + MapDispatchToPropsFunction | TDispatchProps; interface MapDispatchToPropsFactory { (dispatch: Dispatch, ownProps: TOwnProps): MapDispatchToProps; @@ -180,13 +176,13 @@ interface Options extends C * @default strictEqual */ areStatesEqual?: (nextState: any, prevState: any) => boolean; - + /** * When pure, compares incoming props to its previous value. * @default shallowEqual */ areOwnPropsEqual?: (nextOwnProps: TOwnProps, prevOwnProps: TOwnProps) => boolean; - + /** * When pure, compares the result of mapStateToProps to its previous value. * @default shallowEqual diff --git a/types/react-redux/react-redux-tests.tsx b/types/react-redux/react-redux-tests.tsx index bb927c3333..5a9b038dc5 100644 --- a/types/react-redux/react-redux-tests.tsx +++ b/types/react-redux/react-redux-tests.tsx @@ -1,7 +1,7 @@ import { Component, ReactElement } from 'react'; import * as React from 'react'; import * as ReactDOM from 'react-dom'; -import { Store, Dispatch, bindActionCreators } from 'redux'; +import { Store, Dispatch, ActionCreator, bindActionCreators } from 'redux'; import { connect, Provider, DispatchProp, MapStateToProps } from 'react-redux'; import objectAssign = require('object-assign'); @@ -549,3 +549,23 @@ namespace TestControlledComponentWithoutDispatchProp { const MyControlledComponent = connect(mapStateToProps)(MyComponent); const MyControlledFuncComponent = connect(mapStateToProps)(MyFuncComponent); } + +namespace TestDispatchToPropsAsObject { + const onClick: ActionCreator<{}> = null; + const mapStateToProps = (state: any) => { + return { + title: state.app.title as string, + }; + }; + const dispatchToProps = { + onClick, + }; + + type Props = { title: string; } & typeof dispatchToProps; + const HeaderComponent: React.StatelessComponent = (props) => { + return

{props.title}

; + } + + const Header = connect(mapStateToProps, dispatchToProps)(HeaderComponent); +
+}