react-redux: add generic for state parameter

This commit is contained in:
Oliver Joseph Ash
2017-11-10 10:51:55 +00:00
parent 68cd19ffcf
commit 873eb12c02
2 changed files with 37 additions and 37 deletions

View File

@@ -78,67 +78,67 @@ export type InferableComponentEnhancer<TInjectedProps> =
export interface Connect {
(): InferableComponentEnhancer<DispatchProp<any>>;
<TStateProps = {}, no_dispatch = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>
<State, TStateProps = {}, no_dispatch = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<State, TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & DispatchProp<any>, TOwnProps>;
<no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
<State, no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>
): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;
<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
<State, TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<State, TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;
<TStateProps = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
<State, TStateProps = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<State, TStateProps, TOwnProps>,
mapDispatchToProps: null | undefined,
mergeProps: MergeProps<TStateProps, undefined, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
<no_state = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
<State, no_state = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: MergeProps<undefined, TDispatchProps, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
<no_state = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}>(
<State, no_state = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: null | undefined,
mergeProps: MergeProps<undefined, undefined, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
<State, TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<State, TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
<TStateProps = {}, no_dispatch = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
<State, TStateProps = {}, no_dispatch = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<State, TStateProps, TOwnProps>,
mapDispatchToProps: null | undefined,
mergeProps: null | undefined,
options: Options<TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<DispatchProp<any> & TStateProps, TOwnProps>;
<no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
<State, no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: null | undefined,
options: Options<no_state, TOwnProps>
): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;
<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
<State, TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<State, TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: null | undefined,
options: Options<TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;
<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
<State, TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<State, TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
options: Options<TStateProps, TOwnProps, TMergedProps>
@@ -150,15 +150,15 @@ export interface Connect {
*/
export declare const connect: Connect;
interface MapStateToProps<TStateProps, TOwnProps> {
(state: any, ownProps: TOwnProps): TStateProps;
interface MapStateToProps<State, TStateProps, TOwnProps> {
(state: State, ownProps: TOwnProps): TStateProps;
}
interface MapStateToPropsFactory<TStateProps, TOwnProps> {
(initialState: any, ownProps: TOwnProps): MapStateToProps<TStateProps, TOwnProps>;
interface MapStateToPropsFactory<State, TStateProps, TOwnProps> {
(initialState: State, ownProps: TOwnProps): MapStateToProps<State, TStateProps, TOwnProps>;
}
type MapStateToPropsParam<TStateProps, TOwnProps> = MapStateToPropsFactory<TStateProps, TOwnProps> | MapStateToProps<TStateProps, TOwnProps> | null | undefined;
type MapStateToPropsParam<State, TStateProps, TOwnProps> = MapStateToPropsFactory<State, TStateProps, TOwnProps> | MapStateToProps<State, TStateProps, TOwnProps> | null | undefined;
interface MapDispatchToPropsFunction<TDispatchProps, TOwnProps> {
(dispatch: Dispatch<any>, ownProps: TOwnProps): TDispatchProps;
@@ -177,7 +177,7 @@ interface MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps> {
(stateProps: TStateProps, dispatchProps: TDispatchProps, ownProps: TOwnProps): TMergedProps;
}
interface Options<TStateProps = {}, TOwnProps = {}, TMergedProps = {}> extends ConnectOptions {
interface Options<State = {}, TStateProps = {}, TOwnProps = {}, TMergedProps = {}> extends ConnectOptions {
/**
* If true, implements shouldComponentUpdate and shallowly compares the result of mergeProps,
* preventing unnecessary updates, assuming that the component is a “pure” component
@@ -191,7 +191,7 @@ interface Options<TStateProps = {}, TOwnProps = {}, TMergedProps = {}> extends C
* When pure, compares incoming store state to its previous value.
* @default strictEqual
*/
areStatesEqual?: (nextState: any, prevState: any) => boolean;
areStatesEqual?: (nextState: State, prevState: State) => boolean;
/**
* When pure, compares incoming props to its previous value.

View File

@@ -316,12 +316,12 @@ interface ICounterDispatchProps {
onIncrement: () => void
}
// with higher order functions
connect<ICounterStateProps, ICounterDispatchProps>(
connect<CounterState, ICounterStateProps, ICounterDispatchProps>(
() => mapStateToProps,
() => mapDispatchToProps
)(Counter);
// with higher order functions using parameters
connect<ICounterStateProps, ICounterDispatchProps, {}>(
connect<CounterState, ICounterStateProps, ICounterDispatchProps, {}>(
(initialState: CounterState, ownProps) => mapStateToProps,
(dispatch: Dispatch<CounterState>, ownProps) => mapDispatchToProps
)(Counter);
@@ -330,12 +330,12 @@ connect<ICounterStateProps>(
() => mapStateToProps
)(Counter);
// wrap only one argument
connect<ICounterStateProps, ICounterDispatchProps>(
connect<CounterState, ICounterStateProps, ICounterDispatchProps>(
mapStateToProps,
() => mapDispatchToProps
)(Counter);
// with extra arguments
connect<ICounterStateProps, ICounterDispatchProps, {}, ICounterStateProps & ICounterDispatchProps>(
connect<CounterState, ICounterStateProps, ICounterDispatchProps, {}, ICounterStateProps & ICounterDispatchProps>(
() => mapStateToProps,
() => mapDispatchToProps,
(s: ICounterStateProps, d: ICounterDispatchProps) =>
@@ -621,7 +621,7 @@ namespace TestTOwnPropsInference {
const ConnectedWithoutOwnProps = connect(mapStateToPropsWithoutOwnProps)(OwnPropsComponent);
const ConnectedWithOwnProps = connect(mapStateToPropsWithOwnProps)(OwnPropsComponent);
const ConnectedWithTypeHint = connect<StateProps, void, OwnProps>(mapStateToPropsWithoutOwnProps)(OwnPropsComponent);
const ConnectedWithTypeHint = connect<CounterState, StateProps, void, OwnProps>(mapStateToPropsWithoutOwnProps)(OwnPropsComponent);
// This should not compile, which is good.
// React.createElement(ConnectedWithoutOwnProps, { anything: 'goes!' });
@@ -652,7 +652,7 @@ namespace TestTOwnPropsInference {
type PickedOwnProps = Pick<AllProps, "own">
type PickedStateProps = Pick<AllProps, "state">
const mapStateToPropsForPicked: MapStateToProps<PickedStateProps, PickedOwnProps> = (state: any): PickedStateProps => {
const mapStateToPropsForPicked: MapStateToProps<CounterState, PickedStateProps, PickedOwnProps> = (state: any): PickedStateProps => {
return { state: "string" }
}
const ConnectedWithPickedOwnProps = connect(mapStateToPropsForPicked)(AllPropsComponent);
@@ -691,7 +691,7 @@ namespace TestMergedPropsInference {
return { dispatch: 'string' };
}
const ConnectedWithOwnAndState: React.ComponentClass<OwnProps> = connect<StateProps, void, OwnProps, MergedProps>(
const ConnectedWithOwnAndState: React.ComponentClass<OwnProps> = connect<CounterState, StateProps, void, OwnProps, MergedProps>(
mapStateToProps,
undefined,
(stateProps: StateProps) => ({
@@ -699,7 +699,7 @@ namespace TestMergedPropsInference {
}),
)(MergedPropsComponent);
const ConnectedWithOwnAndDispatch: React.ComponentClass<OwnProps> = connect<void, DispatchProps, OwnProps, MergedProps>(
const ConnectedWithOwnAndDispatch: React.ComponentClass<OwnProps> = connect<CounterState, void, DispatchProps, OwnProps, MergedProps>(
undefined,
mapDispatchToProps,
(stateProps: undefined, dispatchProps: DispatchProps) => ({
@@ -707,7 +707,7 @@ namespace TestMergedPropsInference {
}),
)(MergedPropsComponent);
const ConnectedWithOwn: React.ComponentClass<OwnProps> = connect<void, void, OwnProps, MergedProps>(
const ConnectedWithOwn: React.ComponentClass<OwnProps> = connect<CounterState, void, void, OwnProps, MergedProps>(
undefined,
undefined,
() => ({
@@ -733,7 +733,7 @@ namespace Issue16652 {
};
};
const ConnectedCommentList = connect<GeneratedStateProps, {}, PassedProps>(mapStateToProps)(
const ConnectedCommentList = connect<CounterState, GeneratedStateProps, {}, PassedProps>(mapStateToProps)(
CommentList
);
@@ -878,8 +878,8 @@ namespace TestCreateProvider {
interface AProps { a: number };
const A = (props: AProps) => (<h1>A is {props.a}</h1>);
const A1 = connect<AProps>(state => state)(A);
const A2 = myStoreConnect<AProps>(state => state)(A);
const A1 = connect<State, AProps>(state => state)(A);
const A2 = myStoreConnect<State, AProps>(state => state)(A);
const Combined = () => (
<Provider store={store}>