From da8f444b22b9ff1f657d9318a4a191db986bb700 Mon Sep 17 00:00:00 2001 From: Wojtek Czekalski Date: Thu, 2 Aug 2018 18:49:21 +0200 Subject: [PATCH] Fix InferableComponentEnhancerWithProps type inference for StatelessComponent (#27781) * Fix InferableComponentEnhancerWithProps type inference for StatelessComponent The inferred component should require the difference of props supplied by connect and props needed by the component to be passed during render. * Remove ambigous bidirectional type inference --- types/react-redux/index.d.ts | 3 --- types/react-redux/react-redux-tests.tsx | 34 ++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/types/react-redux/index.d.ts b/types/react-redux/index.d.ts index 25b3ae2668..8aa723c232 100644 --- a/types/react-redux/index.d.ts +++ b/types/react-redux/index.d.ts @@ -73,9 +73,6 @@ type Shared< // Will not pass through the injected props if they are passed in during // render. Also adds new prop requirements from TNeedsProps. export interface InferableComponentEnhancerWithProps { - ( - component: StatelessComponent - ): ComponentClass & {WrappedComponent: StatelessComponent}

>( component: ComponentType

): ComponentClass> & TNeedsProps> & {WrappedComponent: ComponentType

} diff --git a/types/react-redux/react-redux-tests.tsx b/types/react-redux/react-redux-tests.tsx index f446df9a1e..5d2395d811 100644 --- a/types/react-redux/react-redux-tests.tsx +++ b/types/react-redux/react-redux-tests.tsx @@ -832,7 +832,12 @@ namespace TestDispatchToPropsAsObject {

} -namespace TestInferredFunctionalComponent { +namespace TestInferredFunctionalComponentWithExplicitOwnProps { + type Props = { + title: string, + extraText: string, + onClick: () => void, + }; const Header = connect( ( @@ -845,7 +850,30 @@ namespace TestInferredFunctionalComponent { (dispatch) => ({ onClick: () => dispatch({ type: 'test' }) }) - )(({ title, extraText, onClick }) => { + )(({ title, extraText, onClick }: Props) => { + return

{title} {extraText}

; + }); +
+} + +namespace TestInferredFunctionalComponentWithImplicitOwnProps { + + type Props = { + title: string, + extraText: string, + onClick: () => void, + }; + + const Header = connect( + ( + { app: { title }}: { app: { title: string }}, + ) => ({ + title, + }), + (dispatch) => ({ + onClick: () => dispatch({ type: 'test' }) + }) + )(({ title, extraText, onClick }: Props) => { return

{title} {extraText}

; });
@@ -1126,4 +1154,4 @@ namespace TestFailsMoreSpecificInjectedProps { // Since it is possible the injected props could fail to satisfy the decoration props, // the following line should fail to compile. connect(mapStateToProps, mapDispatchToProps)(Component) // $ExpectError -} \ No newline at end of file +}