Merge pull request #31799 from VincentLanglet/patchReactRedux

[react-redux] Improve Selector type to handle function with no OwnProps
This commit is contained in:
Benjamin Lichtman
2019-01-24 14:33:58 -08:00
committed by GitHub
2 changed files with 19 additions and 2 deletions
+3 -1
View File
@@ -352,7 +352,9 @@ export function connectAdvanced<S, TProps, TOwnProps, TFactoryOptions = {}>(
export type SelectorFactory<S, TProps, TOwnProps, TFactoryOptions> =
(dispatch: Dispatch<Action>, factoryOptions: TFactoryOptions) => Selector<S, TProps, TOwnProps>;
export type Selector<S, TProps, TOwnProps> = (state: S, ownProps: TOwnProps) => TProps;
export type Selector<S, TProps, TOwnProps = null> = TOwnProps extends null | undefined
? (state: S) => TProps
: (state: S, ownProps: TOwnProps) => TProps;
export interface ConnectOptions {
/**
+16 -1
View File
@@ -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.
<Provider store={store} context={null} />; // $ExpectError
}
function TestSelector() {
interface OwnProps { key?: string; }
interface State { key: string; }
const simpleSelect: Selector<State, string> = (state: State) => state.key;
const notSimpleSelect: Selector<State, string, OwnProps> = (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
}