diff --git a/types/react-redux/index.d.ts b/types/react-redux/index.d.ts index 542894cb6f..5adeb43612 100644 --- a/types/react-redux/index.d.ts +++ b/types/react-redux/index.d.ts @@ -352,7 +352,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 | undefined + ? (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 36cbe588ec..ee5b9c1d94 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, 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'); // @@ -1192,3 +1192,18 @@ function TestProviderContext() { // Null is not a valid value for the context. ; // $ExpectError } + +function TestSelector() { + interface OwnProps { key?: string; } + interface State { key: string; } + + const simpleSelect: Selector = (state: State) => state.key; + const notSimpleSelect: Selector = (state: State, ownProps: OwnProps) => ownProps.key || state.key; + + const ownProps = {}; + const state = { key: 'value' }; + simpleSelect(state); + notSimpleSelect(state, ownProps); + simpleSelect(state, ownProps); // $ExpectError + notSimpleSelect(state); // $ExpectError +}