From 68a4f2b78671329e10bfca87f8f1c82cc8690459 Mon Sep 17 00:00:00 2001 From: Tim Dorr Date: Mon, 15 Aug 2016 11:51:27 -0400 Subject: [PATCH 01/16] Remove Style section Obviously that didn't happen... --- CONTRIBUTING.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 342e92d5d..66c07e803 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -54,10 +54,6 @@ npm run lint Please open an issue with a proposal for a new feature or refactoring before starting on the work. We don't want you to waste your efforts on a pull request that we won't want to accept. -###Style - -[reactjs](https://github.com/reactjs) is trying to keep a standard style across its various projects, which can be found over in [eslint-config-reactjs](https://github.com/reactjs/eslint-config-reactjs). If you have a style change proposal, it should first be proposed there. If accepted, we will be happy to accept a PR to implement it here. - ## Submitting Changes * Open a new issue in the [Issue tracker](https://github.com/reactjs/react-redux/issues). From 6a68d1073c6773ae12c4b71f125c8def39b8fb2f Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Mon, 7 Nov 2016 19:18:12 +0100 Subject: [PATCH 02/16] add typings --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 576522b83..af832b523 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "4.4.5", "description": "Official React bindings for Redux", "main": "./lib/index.js", + "typings": "./index.d.ts", "scripts": { "build:lib": "babel src --out-dir lib", "build:umd": "cross-env NODE_ENV=development webpack src/index.js dist/react-redux.js", @@ -22,7 +23,8 @@ "files": [ "dist", "lib", - "src" + "src", + "index.d.ts" ], "keywords": [ "react", From 722e1b116cc542788a6e88dc38dd5d5419058e4c Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Mon, 7 Nov 2016 19:18:30 +0100 Subject: [PATCH 03/16] add typings --- index.d.ts | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 000000000..130becd40 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,66 @@ +import { ComponentClass, Component, StatelessComponent, ReactNode } from 'react'; +import { Store, Dispatch, ActionCreator } from 'redux'; + +interface ComponentDecorator { + (component: ComponentClass|StatelessComponent): ComponentClass; +} + +/** + * Following 3 functions cover all possible ways connect could be invoked + * + * - State: Redux state interface (the same one used by Store) + * - TStateProps: Result of MapStateToProps + * - TDispatchProps: Result of MapDispatchToProps + * - TOwnProps: Props passed to the wrapping component + */ +export function connect(): ComponentDecorator<{ dispatch: Dispatch }, TOwnProps>; + +export function connect( + mapStateToProps: MapStateToProps, + mapDispatchToProps?: MapDispatchToPropsFunction|MapDispatchToPropsObject +): ComponentDecorator; + +export function connect( + mapStateToProps: MapStateToProps, + mapDispatchToProps: MapDispatchToPropsFunction|MapDispatchToPropsObject, + mergeProps: MergeProps, + options?: Options +): ComponentDecorator; + +interface MapStateToProps { + (state: State, ownProps?: TOwnProps): TStateProps; +} + +/** + * State is not actually used here but included for consistency with Redux typings and MapStateToProps. + */ +interface MapDispatchToPropsFunction { + (dispatch: Dispatch, ownProps?: TOwnProps): TDispatchProps; +} + +/** + * Any since not every ActionCreator returns the same Action + */ +interface MapDispatchToPropsObject { + [name: string]: ActionCreator; +} + +interface MergeProps { + (stateProps: TStateProps, dispatchProps: TDispatchProps, ownProps: TOwnProps): TStateProps & TDispatchProps; +} + +interface Options { + pure?: boolean; + withRef?: boolean; +} + +/** + * Typescript does not support generic components in tsx yet in an intuïtive way which is the reason we avoid a + * generic parameter in Store here by using any as the type + */ +export interface ProviderProps { + store?: Store; + children?: ReactNode; +} + +export class Provider extends Component { } From ec99f1d882dfcc5a90f556a80233f173d2ac3edc Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Mon, 7 Nov 2016 20:52:57 +0100 Subject: [PATCH 04/16] first templates order and mapStateToProps --- index.d.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index 130becd40..291013d05 100644 --- a/index.d.ts +++ b/index.d.ts @@ -13,14 +13,18 @@ interface ComponentDecorator { * - TDispatchProps: Result of MapDispatchToProps * - TOwnProps: Props passed to the wrapping component */ -export function connect(): ComponentDecorator<{ dispatch: Dispatch }, TOwnProps>; +export function connect(): ComponentDecorator<{ dispatch: Dispatch }, TOwnProps>; -export function connect( +export function connect( mapStateToProps: MapStateToProps, - mapDispatchToProps?: MapDispatchToPropsFunction|MapDispatchToPropsObject +): ComponentDecorator }, TOwnProps>; + +export function connect( + mapStateToProps: MapStateToProps, + mapDispatchToProps: MapDispatchToPropsFunction|MapDispatchToPropsObject ): ComponentDecorator; -export function connect( +export function connect( mapStateToProps: MapStateToProps, mapDispatchToProps: MapDispatchToPropsFunction|MapDispatchToPropsObject, mergeProps: MergeProps, @@ -28,7 +32,7 @@ export function connect( ): ComponentDecorator; interface MapStateToProps { - (state: State, ownProps?: TOwnProps): TStateProps; + (state: State, ownProps: TOwnProps): TStateProps; } /** From 52db71a0450a86ec8d9d58a6cce4fb482753cdb3 Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Mon, 7 Nov 2016 23:01:16 +0100 Subject: [PATCH 05/16] fix template order and add second's API use case (no store subscription) --- index.d.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.d.ts b/index.d.ts index 291013d05..53a35b9d6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -20,13 +20,13 @@ export function connect( ): ComponentDecorator }, TOwnProps>; export function connect( - mapStateToProps: MapStateToProps, - mapDispatchToProps: MapDispatchToPropsFunction|MapDispatchToPropsObject + mapStateToProps: MapStateToProps|null, + mapDispatchToProps: MapDispatchToPropsFunction|TDispatchProps ): ComponentDecorator; export function connect( - mapStateToProps: MapStateToProps, - mapDispatchToProps: MapDispatchToPropsFunction|MapDispatchToPropsObject, + mapStateToProps: MapStateToProps|null, + mapDispatchToProps: MapDispatchToPropsFunction|TDispatchProps, mergeProps: MergeProps, options?: Options ): ComponentDecorator; @@ -39,14 +39,14 @@ interface MapStateToProps { * State is not actually used here but included for consistency with Redux typings and MapStateToProps. */ interface MapDispatchToPropsFunction { - (dispatch: Dispatch, ownProps?: TOwnProps): TDispatchProps; + (dispatch: Dispatch, ownProps: TOwnProps): TDispatchProps; } /** * Any since not every ActionCreator returns the same Action */ -interface MapDispatchToPropsObject { - [name: string]: ActionCreator; +interface MapDispatchToPropsObject { + [name: string]: ActionCreator; } interface MergeProps { From d8f43948770c196be8a84395d325973ebfbc8cf6 Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Tue, 8 Nov 2016 13:00:27 +0100 Subject: [PATCH 06/16] remove unnecessary children prop and optional flag for store --- index.d.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 53a35b9d6..b09572090 100644 --- a/index.d.ts +++ b/index.d.ts @@ -63,8 +63,7 @@ interface Options { * generic parameter in Store here by using any as the type */ export interface ProviderProps { - store?: Store; - children?: ReactNode; + store: Store; } export class Provider extends Component { } From 5793e0e6b6f0e158fd8be97402b3a029474ddae5 Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Tue, 8 Nov 2016 13:20:56 +0100 Subject: [PATCH 07/16] add specific case for connect(null, mapDispatchToProps) --- index.d.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index b09572090..67ccd040b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -16,18 +16,23 @@ interface ComponentDecorator { export function connect(): ComponentDecorator<{ dispatch: Dispatch }, TOwnProps>; export function connect( - mapStateToProps: MapStateToProps, + mapStateToProps: MapStateToProps, ): ComponentDecorator }, TOwnProps>; export function connect( - mapStateToProps: MapStateToProps|null, - mapDispatchToProps: MapDispatchToPropsFunction|TDispatchProps + mapStateToProps: MapStateToProps, + mapDispatchToProps: MapDispatchToPropsFunction|TDispatchProps ): ComponentDecorator; +export function connect( + mapStateToProps: null, + mapDispatchToProps: MapDispatchToPropsFunction|TDispatchProps +): ComponentDecorator; + export function connect( - mapStateToProps: MapStateToProps|null, - mapDispatchToProps: MapDispatchToPropsFunction|TDispatchProps, - mergeProps: MergeProps, + mapStateToProps: MapStateToProps, + mapDispatchToProps: MapDispatchToPropsFunction|TDispatchProps, + mergeProps: MergeProps, options?: Options ): ComponentDecorator; From 2e444e8395a5f79bb1e0bbecae1240f84a628da2 Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Tue, 8 Nov 2016 13:24:01 +0100 Subject: [PATCH 08/16] have StatelessComponent as the first type --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 67ccd040b..be9398e4e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,7 +2,7 @@ import { ComponentClass, Component, StatelessComponent, ReactNode } from 'react' import { Store, Dispatch, ActionCreator } from 'redux'; interface ComponentDecorator { - (component: ComponentClass|StatelessComponent): ComponentClass; + (component: StatelessComponent|ComponentClass): ComponentClass; } /** From 92326dd10ee8e6952dec89435fbb98018d9f4e61 Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Tue, 8 Nov 2016 13:57:33 +0100 Subject: [PATCH 09/16] respect template parameters order in comments --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index be9398e4e..41ed3f018 100644 --- a/index.d.ts +++ b/index.d.ts @@ -9,9 +9,9 @@ interface ComponentDecorator { * Following 3 functions cover all possible ways connect could be invoked * * - State: Redux state interface (the same one used by Store) + * - TOwnProps: Props passed to the wrapping component * - TStateProps: Result of MapStateToProps * - TDispatchProps: Result of MapDispatchToProps - * - TOwnProps: Props passed to the wrapping component */ export function connect(): ComponentDecorator<{ dispatch: Dispatch }, TOwnProps>; From 4f907f7258af13d23af5a27c21876cf080e38e3f Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Tue, 8 Nov 2016 14:01:29 +0100 Subject: [PATCH 10/16] fix template parameters order (oops, forgot that ones when copy-pasting :/) --- index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 41ed3f018..09bf1308b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -36,14 +36,14 @@ export function connect( options?: Options ): ComponentDecorator; -interface MapStateToProps { +interface MapStateToProps { (state: State, ownProps: TOwnProps): TStateProps; } /** * State is not actually used here but included for consistency with Redux typings and MapStateToProps. */ -interface MapDispatchToPropsFunction { +interface MapDispatchToPropsFunction { (dispatch: Dispatch, ownProps: TOwnProps): TDispatchProps; } @@ -54,8 +54,8 @@ interface MapDispatchToPropsObject { [name: string]: ActionCreator; } -interface MergeProps { - (stateProps: TStateProps, dispatchProps: TDispatchProps, ownProps: TOwnProps): TStateProps & TDispatchProps; +interface MergeProps { + (ownProps: TOwnProps, stateProps: TStateProps, dispatchProps: TDispatchProps): TStateProps & TDispatchProps; } interface Options { From 64b201e497c9d6a7471b0c968ab8504cbed1e838 Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Tue, 8 Nov 2016 14:16:36 +0100 Subject: [PATCH 11/16] remove some dead code --- index.d.ts | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/index.d.ts b/index.d.ts index 09bf1308b..12360ec99 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,7 +6,7 @@ interface ComponentDecorator { } /** - * Following 3 functions cover all possible ways connect could be invoked + * Following 5 functions cover all possible ways connect could be invoked * * - State: Redux state interface (the same one used by Store) * - TOwnProps: Props passed to the wrapping component @@ -40,20 +40,10 @@ interface MapStateToProps { (state: State, ownProps: TOwnProps): TStateProps; } -/** - * State is not actually used here but included for consistency with Redux typings and MapStateToProps. - */ interface MapDispatchToPropsFunction { (dispatch: Dispatch, ownProps: TOwnProps): TDispatchProps; } -/** - * Any since not every ActionCreator returns the same Action - */ -interface MapDispatchToPropsObject { - [name: string]: ActionCreator; -} - interface MergeProps { (ownProps: TOwnProps, stateProps: TStateProps, dispatchProps: TDispatchProps): TStateProps & TDispatchProps; } From 63655a9a913b5f556ec4b094b00b03b281955a6c Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Tue, 8 Nov 2016 14:27:07 +0100 Subject: [PATCH 12/16] add TMergeProps generic --- index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 12360ec99..79270d795 100644 --- a/index.d.ts +++ b/index.d.ts @@ -29,10 +29,10 @@ export function connect( mapDispatchToProps: MapDispatchToPropsFunction|TDispatchProps ): ComponentDecorator; -export function connect( +export function connect( mapStateToProps: MapStateToProps, mapDispatchToProps: MapDispatchToPropsFunction|TDispatchProps, - mergeProps: MergeProps, + mergeProps: MergeProps, options?: Options ): ComponentDecorator; @@ -44,8 +44,8 @@ interface MapDispatchToPropsFunction { (dispatch: Dispatch, ownProps: TOwnProps): TDispatchProps; } -interface MergeProps { - (ownProps: TOwnProps, stateProps: TStateProps, dispatchProps: TDispatchProps): TStateProps & TDispatchProps; +interface MergeProps { + (ownProps: TOwnProps, stateProps: TStateProps, dispatchProps: TDispatchProps): TMergeProps; } interface Options { From a6e067824edbf07964d82a1e1dabe7f86710bf13 Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Tue, 8 Nov 2016 14:33:24 +0100 Subject: [PATCH 13/16] mapState / mapDispatch can be factory functions (stolen from DT last version) --- index.d.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 79270d795..480a8c877 100644 --- a/index.d.ts +++ b/index.d.ts @@ -16,22 +16,22 @@ interface ComponentDecorator { export function connect(): ComponentDecorator<{ dispatch: Dispatch }, TOwnProps>; export function connect( - mapStateToProps: MapStateToProps, + mapStateToProps: FuncOrSelf>, ): ComponentDecorator }, TOwnProps>; export function connect( - mapStateToProps: MapStateToProps, - mapDispatchToProps: MapDispatchToPropsFunction|TDispatchProps + mapStateToProps: FuncOrSelf>, + mapDispatchToProps: FuncOrSelf|TDispatchProps> ): ComponentDecorator; export function connect( mapStateToProps: null, - mapDispatchToProps: MapDispatchToPropsFunction|TDispatchProps + mapDispatchToProps: FuncOrSelf|TDispatchProps> ): ComponentDecorator; export function connect( - mapStateToProps: MapStateToProps, - mapDispatchToProps: MapDispatchToPropsFunction|TDispatchProps, + mapStateToProps: FuncOrSelf>, + mapDispatchToProps: FuncOrSelf|TDispatchProps>, mergeProps: MergeProps, options?: Options ): ComponentDecorator; @@ -53,6 +53,8 @@ interface Options { withRef?: boolean; } +type FuncOrSelf = T | (() => T); + /** * Typescript does not support generic components in tsx yet in an intuïtive way which is the reason we avoid a * generic parameter in Store here by using any as the type From 670c85146170ff712587782303a2f7afbe5fbda3 Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Tue, 8 Nov 2016 14:54:02 +0100 Subject: [PATCH 14/16] make sure mapDispatchToProps returns TDispatchProps that is ALSO a hash of action creators --- index.d.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 480a8c877..611ef6b01 100644 --- a/index.d.ts +++ b/index.d.ts @@ -21,21 +21,25 @@ export function connect( export function connect( mapStateToProps: FuncOrSelf>, - mapDispatchToProps: FuncOrSelf|TDispatchProps> + mapDispatchToProps: FuncOrSelf | MapDispatchToPropsObject & TDispatchProps> ): ComponentDecorator; export function connect( mapStateToProps: null, - mapDispatchToProps: FuncOrSelf|TDispatchProps> + mapDispatchToProps: FuncOrSelf | MapDispatchToPropsObject & TDispatchProps> ): ComponentDecorator; export function connect( mapStateToProps: FuncOrSelf>, - mapDispatchToProps: FuncOrSelf|TDispatchProps>, + mapDispatchToProps: FuncOrSelf | MapDispatchToPropsObject & TDispatchProps>, mergeProps: MergeProps, options?: Options ): ComponentDecorator; +interface MapDispatchToPropsObject { + [name: string]: ActionCreator; +} + interface MapStateToProps { (state: State, ownProps: TOwnProps): TStateProps; } From d3519d43c8dd14ef7cbacb501266d834b0bc1dfe Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Tue, 8 Nov 2016 15:23:05 +0100 Subject: [PATCH 15/16] remove unused import --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 611ef6b01..c43be480a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,4 @@ -import { ComponentClass, Component, StatelessComponent, ReactNode } from 'react'; +import { ComponentClass, Component, StatelessComponent } from 'react'; import { Store, Dispatch, ActionCreator } from 'redux'; interface ComponentDecorator { From d3f6f243ab66127156245bf5b35b83575864061e Mon Sep 17 00:00:00 2001 From: Benoit Benezech Date: Wed, 9 Nov 2016 14:08:12 +0100 Subject: [PATCH 16/16] fix argument order for mergeProps (duh) and ComponentDecorator arguments --- index.d.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/index.d.ts b/index.d.ts index c43be480a..9c1da0490 100644 --- a/index.d.ts +++ b/index.d.ts @@ -13,28 +13,28 @@ interface ComponentDecorator { * - TStateProps: Result of MapStateToProps * - TDispatchProps: Result of MapDispatchToProps */ -export function connect(): ComponentDecorator<{ dispatch: Dispatch }, TOwnProps>; +function connect(): ComponentDecorator<{ dispatch: Dispatch } & TOwnProps, TOwnProps>; -export function connect( +function connect( mapStateToProps: FuncOrSelf>, -): ComponentDecorator }, TOwnProps>; +): ComponentDecorator } & TOwnProps, TOwnProps>; -export function connect( +function connect( mapStateToProps: FuncOrSelf>, mapDispatchToProps: FuncOrSelf | MapDispatchToPropsObject & TDispatchProps> -): ComponentDecorator; +): ComponentDecorator; -export function connect( +function connect( mapStateToProps: null, mapDispatchToProps: FuncOrSelf | MapDispatchToPropsObject & TDispatchProps> -): ComponentDecorator; +): ComponentDecorator; -export function connect( +function connect( mapStateToProps: FuncOrSelf>, - mapDispatchToProps: FuncOrSelf | MapDispatchToPropsObject & TDispatchProps>, + mapDispatchToProps: FuncOrSelf| MapDispatchToPropsObject & TDispatchProps>, mergeProps: MergeProps, options?: Options -): ComponentDecorator; +): ComponentDecorator; interface MapDispatchToPropsObject { [name: string]: ActionCreator; @@ -49,7 +49,7 @@ interface MapDispatchToPropsFunction { } interface MergeProps { - (ownProps: TOwnProps, stateProps: TStateProps, dispatchProps: TDispatchProps): TMergeProps; + (stateProps: TStateProps, dispatchProps: TDispatchProps, ownProps: TOwnProps): TMergeProps; } interface Options {