From 610fbf78ced65aba337029eb61be48dacc4a1010 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 31 Dec 2018 15:11:39 +0100 Subject: [PATCH 1/5] Improve selector type --- types/react-redux/index.d.ts | 4 +++- types/react-redux/react-redux-tests.tsx | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/types/react-redux/index.d.ts b/types/react-redux/index.d.ts index bcbcced0bd..265a7daa49 100644 --- a/types/react-redux/index.d.ts +++ b/types/react-redux/index.d.ts @@ -343,7 +343,9 @@ export function connectAdvanced( export type SelectorFactory = (dispatch: Dispatch, factoryOptions: TFactoryOptions) => Selector; -export type Selector = (state: S, ownProps: TOwnProps) => TProps; +export type Selector = TOwnProps extends null + ? (state: S) => TProps + : (state: S, ownProps: TOwnProps) => TProps; export interface ConnectOptions { /** diff --git a/types/react-redux/react-redux-tests.tsx b/types/react-redux/react-redux-tests.tsx index 554ace09ff..75d524ce1b 100644 --- a/types/react-redux/react-redux-tests.tsx +++ b/types/react-redux/react-redux-tests.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import * as ReactDOM from 'react-dom'; import { Store, Dispatch, AnyAction, ActionCreator, createStore, bindActionCreators, ActionCreatorsMapObject, Reducer } from 'redux'; -import { Connect, connect, createProvider, Provider, DispatchProp, MapStateToProps, Options } from 'react-redux'; +import { Connect, connect, createProvider, Provider, DispatchProp, MapStateToProps, Options, Selector } from 'react-redux'; import objectAssign = require('object-assign'); // @@ -1210,3 +1210,16 @@ function TestLibraryManagedAttributes() { const ConnectedComponent2 = connect(mapStateToProps)(Component); {}} />; } + +function TestSelector() { + interface OwnProps { key?: string; } + interface State { key: string; } + + const simpleSelect: Selector = (state: State) => state.key; + const notSimpleSelect: Selector = (state: State, ownProps) => ownProps.key || state.key; + + const ownProps = {}; + const state = { key: 'value' }; + simpleSelect(state); + notSimpleSelect(state, ownProps); +} From 912a0401282de48b4f9b84cfe367110b1c566793 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 12 Jan 2019 13:32:08 +0100 Subject: [PATCH 2/5] Add missing type --- types/react-redux/react-redux-tests.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/react-redux/react-redux-tests.tsx b/types/react-redux/react-redux-tests.tsx index 75d524ce1b..9ba6107cd1 100644 --- a/types/react-redux/react-redux-tests.tsx +++ b/types/react-redux/react-redux-tests.tsx @@ -1216,7 +1216,7 @@ function TestSelector() { interface State { key: string; } const simpleSelect: Selector = (state: State) => state.key; - const notSimpleSelect: Selector = (state: State, ownProps) => ownProps.key || state.key; + const notSimpleSelect: Selector = (state: State, ownProps: OwnProps) => ownProps.key || state.key; const ownProps = {}; const state = { key: 'value' }; From cfc7e73a3dd08923632e264a2be60f1ca63d7539 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 16 Jan 2019 11:04:40 +0100 Subject: [PATCH 3/5] Fix --- types/react-redux/react-redux-tests.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/react-redux/react-redux-tests.tsx b/types/react-redux/react-redux-tests.tsx index 3fdd3d089a..36f1a63fb2 100644 --- a/types/react-redux/react-redux-tests.tsx +++ b/types/react-redux/react-redux-tests.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import * as ReactDOM from 'react-dom'; import { Store, Dispatch, AnyAction, ActionCreator, createStore, bindActionCreators, ActionCreatorsMapObject, Reducer } from 'redux'; -import { Connect, connect, createProvider, Provider, DispatchProp, MapStateToProps, Options, ReactReduxContext, ReactReduxContextValue } from 'react-redux'; +import { Connect, connect, Provider, DispatchProp, MapStateToProps, Options, ReactReduxContext, ReactReduxContextValue, Selector } from 'react-redux'; import objectAssign = require('object-assign'); // From 2b40beb29018078fb525ab6123fa4366f9ace364 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 24 Jan 2019 22:13:38 +0100 Subject: [PATCH 4/5] Update index.d.ts --- types/react-redux/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/react-redux/index.d.ts b/types/react-redux/index.d.ts index ea6c1c71c8..5adeb43612 100644 --- a/types/react-redux/index.d.ts +++ b/types/react-redux/index.d.ts @@ -352,7 +352,7 @@ export function connectAdvanced( export type SelectorFactory = (dispatch: Dispatch, factoryOptions: TFactoryOptions) => Selector; -export type Selector = TOwnProps extends null +export type Selector = TOwnProps extends null | undefined ? (state: S) => TProps : (state: S, ownProps: TOwnProps) => TProps; From 3beeb8c77c07a7b153b8fd27df50b816d38c9f71 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 24 Jan 2019 22:14:06 +0100 Subject: [PATCH 5/5] Update react-redux-tests.tsx --- types/react-redux/react-redux-tests.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/react-redux/react-redux-tests.tsx b/types/react-redux/react-redux-tests.tsx index 36f1a63fb2..ee5b9c1d94 100644 --- a/types/react-redux/react-redux-tests.tsx +++ b/types/react-redux/react-redux-tests.tsx @@ -1204,4 +1204,6 @@ function TestSelector() { const state = { key: 'value' }; simpleSelect(state); notSimpleSelect(state, ownProps); + simpleSelect(state, ownProps); // $ExpectError + notSimpleSelect(state); // $ExpectError }