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
This commit is contained in:
Wojtek Czekalski
2018-08-02 09:49:21 -07:00
committed by Sheetal Nandi
parent 6f8d56fbbf
commit da8f444b22
2 changed files with 31 additions and 6 deletions
-3
View File
@@ -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<TInjectedProps, TNeedsProps> {
(
component: StatelessComponent<TInjectedProps>
): ComponentClass<TNeedsProps> & {WrappedComponent: StatelessComponent<TInjectedProps>}
<P extends Shared<TInjectedProps, P>>(
component: ComponentType<P>
): ComponentClass<Omit<P, keyof Shared<TInjectedProps, P>> & TNeedsProps> & {WrappedComponent: ComponentType<P>}
+31 -3
View File
@@ -832,7 +832,12 @@ namespace TestDispatchToPropsAsObject {
<Header />
}
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 <h1 onClick={onClick}>{title} {extraText}</h1>;
});
<Header extraText='text'/>
}
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 <h1 onClick={onClick}>{title} {extraText}</h1>;
});
<Header extraText='text'/>
@@ -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
}
}