Fixed the typings for inferred types with functional components

This commit is contained in:
waterfoul
2018-05-18 08:56:09 -05:00
parent 0e3cd2e17c
commit 57cb3558e2
2 changed files with 43 additions and 21 deletions

View File

@@ -1,4 +1,4 @@
// Type definitions for react-redux 6.0.0
// Type definitions for react-redux 6.0.1
// Project: https://github.com/rackt/react-redux
// Definitions by: Qubo <https://github.com/tkqubo>,
// Thomas Hasner <https://github.com/thasner>,
@@ -54,7 +54,7 @@ interface AdvancedComponentDecorator<TProps, TOwnProps> {
* - it is present in both DecorationTargetProps and InjectedProps
* - DecorationTargetProps[P] extends InjectedProps[P]
* ie: decorated component can accept more types than decorator is injecting
*
*
* For decoration, inject props or ownProps are all optionnaly
* required by the decorated (right hand side) component.
* But any property required by the decorated component must extend the injected property
@@ -70,9 +70,12 @@ 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> {
<P extends Shared<TInjectedProps, P>>(
component: Component<P>
): ComponentClass<Omit<P, keyof Shared<TInjectedProps, P>> & TNeedsProps> & {WrappedComponent: Component<P>}
(
component: StatelessComponent<TInjectedProps>
): ComponentClass<TNeedsProps> & {WrappedComponent: StatelessComponent<TInjectedProps>}
<P extends Shared<TInjectedProps, P>>(
component: Component<P>
): ComponentClass<Omit<P, keyof Shared<TInjectedProps, P>> & TNeedsProps> & {WrappedComponent: Component<P>}
}
// Injects props and removes them from the prop requirements.

View File

@@ -813,23 +813,42 @@ namespace TestControlledComponentWithoutDispatchProp {
}
namespace TestDispatchToPropsAsObject {
const onClick: ActionCreator<{}> = () => ({});
const mapStateToProps = (state: any) => {
return {
title: state.app.title as string,
};
};
const dispatchToProps = {
onClick,
};
const onClick: ActionCreator<{}> = () => ({});
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> = (props) => {
return <h1>{props.title}</h1>;
}
type Props = { title: string; } & typeof dispatchToProps;
const HeaderComponent: React.StatelessComponent<Props> = (props) => {
return <h1>{props.title}</h1>;
}
const Header = connect(mapStateToProps, dispatchToProps)(HeaderComponent);
<Header />
const Header = connect(mapStateToProps, dispatchToProps)(HeaderComponent);
<Header />
}
namespace TestInferredFunctionalComponent {
const Header = connect(
(
{ app: { title }}: { app: { title: string }},
{ extraText }: { extraText: string }
) => ({
title,
extraText
}),
(dispatch) => ({
onClick: () => dispatch({ type: 'test' })
})
)(({ title, extraText, onClick }) => {
return <h1 onClick={onClick}>{title} {extraText}</h1>;
});
<Header extraText='text'/>
}
namespace TestWrappedComponent {
@@ -933,7 +952,7 @@ namespace TestWithoutTOwnPropsDecoratedInference {
// This should compile
React.createElement(ConnectedWithOwnPropsClass, { own: 'string', forwarded: 'string' });
React.createElement(ConnectedWithOwnPropsClass, { own: 'string', forwarded: 'string' });
// This should not compile, it is missing ForwardedProps
React.createElement(ConnectedWithOwnPropsClass, { own: 'string' }); // $ExpectError
React.createElement(ConnectedWithOwnPropsStateless, { own: 'string' }); // $ExpectError