Improve selector type

This commit is contained in:
Vincent Langlet
2018-12-31 15:27:28 +01:00
parent 52357dc4ca
commit 610fbf78ce
2 changed files with 17 additions and 2 deletions
+3 -1
View File
@@ -343,7 +343,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
? (state: S) => TProps
: (state: S, ownProps: TOwnProps) => TProps;
export interface ConnectOptions {
/**
+14 -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, 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<MapStateProps, void, OwnProps>(mapStateToProps)(Component);
<ConnectedComponent2 fn={() => {}} />;
}
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.key || state.key;
const ownProps = {};
const state = { key: 'value' };
simpleSelect(state);
notSimpleSelect(state, ownProps);
}