From e4f661ba43610a64ecaf0877ad38cef44313bb7f Mon Sep 17 00:00:00 2001 From: Oliver Joseph Ash Date: Fri, 19 Apr 2019 16:44:41 +0100 Subject: [PATCH] react-redux: correct `mapDispatchToProps` param type (#34498) * react-redux: correct `mapDispatchToProps` param type Fixes https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34493 * Complete explanation * Move test --- types/react-redux/index.d.ts | 8 +++--- types/react-redux/react-redux-tests.tsx | 33 ++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/types/react-redux/index.d.ts b/types/react-redux/index.d.ts index 1b2ababe14..5d9f01249a 100644 --- a/types/react-redux/index.d.ts +++ b/types/react-redux/index.d.ts @@ -171,7 +171,7 @@ export interface Connect { ( mapStateToProps: null | undefined, - mapDispatchToProps: TDispatchProps, + mapDispatchToProps: MapDispatchToPropsParam, ): InferableComponentEnhancerWithProps< ResolveThunks, TOwnProps @@ -184,7 +184,7 @@ export interface Connect { ( mapStateToProps: MapStateToPropsParam, - mapDispatchToProps: TDispatchProps, + mapDispatchToProps: MapDispatchToPropsParam, ): InferableComponentEnhancerWithProps< TStateProps & ResolveThunks, TOwnProps @@ -231,7 +231,7 @@ export interface Connect { ( mapStateToProps: null | undefined, - mapDispatchToProps: TDispatchProps, + mapDispatchToProps: MapDispatchToPropsParam, mergeProps: null | undefined, options: Options<{}, TStateProps, TOwnProps> ): InferableComponentEnhancerWithProps< @@ -248,7 +248,7 @@ export interface Connect { ( mapStateToProps: MapStateToPropsParam, - mapDispatchToProps: TDispatchProps, + mapDispatchToProps: MapDispatchToPropsParam, mergeProps: null | undefined, options: Options ): InferableComponentEnhancerWithProps< diff --git a/types/react-redux/react-redux-tests.tsx b/types/react-redux/react-redux-tests.tsx index 085874a523..95fcbc9dee 100644 --- a/types/react-redux/react-redux-tests.tsx +++ b/types/react-redux/react-redux-tests.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import * as ReactDOM from 'react-dom'; import { Store, Dispatch, AnyAction, ActionCreator, createStore, bindActionCreators, ActionCreatorsMapObject, Reducer } from 'redux'; -import { Connect, connect, Provider, DispatchProp, MapStateToProps, Options, ReactReduxContext, ReactReduxContextValue, Selector } from 'react-redux'; +import { Connect, connect, Provider, DispatchProp, MapStateToProps, Options, ReactReduxContext, ReactReduxContextValue, Selector, MapDispatchToProps } from 'react-redux'; import objectAssign = require('object-assign'); // @@ -82,9 +82,34 @@ function MapDispatch() { class TestComponent extends React.Component { } - const mapDispatchToProps = () => ({ - onClick: () => { } - }); + const mapDispatchToProps = ({ onClick: () => {} }); + + const TestNull = connect( + null, + mapDispatchToProps, + )(TestComponent); + + const verifyNull = ; + + const TestUndefined = connect( + undefined, + mapDispatchToProps, + )(TestComponent); + + const verifyUndefined = ; +} + +function MapDispatchUnion() { + interface OwnProps { foo: string; } + interface DispatchProps { onClick: () => void; } + + class TestComponent extends React.Component { } + + // We deliberately cast the right-hand side to `any` because otherwise + // TypeScript would maintain the literal value, when we deliberately want to + // test the union type here (as per the annotation). See + // https://github.com/Microsoft/TypeScript/issues/30310#issuecomment-472218182. + const mapDispatchToProps: MapDispatchToProps = {} as any; const TestNull = connect( null,