Add StatelessComponent<TOriginalProps> as a union type for ComponentDecorator. It allows StatelessComponents to work with the connect method signature that takes mapStateToProps and mapDispatchToProps functions.

This commit is contained in:
Larry Myers
2016-04-06 20:56:00 +09:00
committed by Masahiro Wakame
parent 15337e2b66
commit 3faa118b04
2 changed files with 32 additions and 1 deletions
+31
View File
@@ -285,6 +285,37 @@ let ConnectedHelloMessage = connect()(HelloMessage);
ReactDOM.render(<HelloMessage name="Sebastian" />, document.getElementById('content'));
ReactDOM.render(<ConnectedHelloMessage name="Sebastian" />, document.getElementById('content'));
// stateless functions that uses mapStateToProps and mapDispatchToProps
namespace TestStatelessFunctionWithMapArguments {
interface GreetingProps {
name: string;
onClick: () => void;
}
function Greeting(props: GreetingProps) {
return <div>Hello {props.name}</div>;
}
const mapStateToProps = (state: any, ownProps: GreetingProps) => {
return {
name: 'Connected! ' + ownProps.name
};
};
const mapDispatchToProps = (dispatch: Dispatch, ownProps: GreetingProps) => {
return {
onClick: () => {
dispatch({ type: 'GREETING', name: ownProps.name });
}
};
};
const ConnectedGreeting = connect(
mapStateToProps,
mapDispatchToProps
)(Greeting);
}
// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/8787
namespace TestTOwnPropsInference {
interface OwnProps {
+1 -1
View File
@@ -11,7 +11,7 @@ declare module "react-redux" {
import { Store, Dispatch, ActionCreator } from 'redux';
interface ComponentDecorator<TOriginalProps, TOwnProps> {
(component: ComponentClass<TOriginalProps>): ComponentClass<TOwnProps>;
(component: ComponentClass<TOriginalProps>|StatelessComponent<TOriginalProps>): ComponentClass<TOwnProps>;
}
/**