From ae2918c2c0537c563e767875239fb15928cbb222 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Sun, 10 Apr 2022 18:46:35 -0400 Subject: [PATCH 1/2] Cleanup internal types in selectorFactory.ts --- src/connect/selectorFactory.ts | 84 ++++++++++++++++------------------ 1 file changed, 40 insertions(+), 44 deletions(-) diff --git a/src/connect/selectorFactory.ts b/src/connect/selectorFactory.ts index 8f3c4b73d..cf39e213b 100644 --- a/src/connect/selectorFactory.ts +++ b/src/connect/selectorFactory.ts @@ -3,34 +3,32 @@ import verifySubselectors from './verifySubselectors' import type { EqualityFn } from '../types' export type SelectorFactory = ( - dispatch: Dispatch, + dispatch: Dispatch>, factoryOptions: TFactoryOptions ) => Selector -export type Selector = TOwnProps extends - | null - | undefined +export type Selector = TOwnProps extends null | undefined ? (state: S) => TProps : (state: S, ownProps: TOwnProps) => TProps -export type MapStateToProps = ( +export type MapStateToProps = ( state: State, ownProps: TOwnProps ) => TStateProps -export type MapStateToPropsFactory = ( +export type MapStateToPropsFactory = ( initialState: State, ownProps: TOwnProps ) => MapStateToProps -export type MapStateToPropsParam = +export type MapStateToPropsParam = | MapStateToPropsFactory | MapStateToProps | null | undefined export type MapDispatchToPropsFunction = ( - dispatch: Dispatch, + dispatch: Dispatch>, ownProps: TOwnProps ) => TDispatchProps @@ -39,7 +37,7 @@ export type MapDispatchToProps = | TDispatchProps export type MapDispatchToPropsFactory = ( - dispatch: Dispatch, + dispatch: Dispatch>, ownProps: TOwnProps ) => MapDispatchToPropsFunction @@ -57,10 +55,10 @@ export type MergeProps = ( ownProps: TOwnProps ) => TMergedProps -interface PureSelectorFactoryComparisonOptions { +interface PureSelectorFactoryComparisonOptions { areStatesEqual: EqualityFn areOwnPropsEqual: EqualityFn - areStatePropsEqual: EqualityFn + areStatePropsEqual: EqualityFn displayName: string } @@ -69,21 +67,17 @@ export function pureFinalPropsSelectorFactory< TOwnProps, TDispatchProps, TMergedProps, - State = unknown + State >( - mapStateToProps: MapStateToPropsParam & { - dependsOnOwnProps: boolean - }, - mapDispatchToProps: MapDispatchToPropsParam & { - dependsOnOwnProps: boolean - }, + mapStateToProps: WrappedMapStateToProps, + mapDispatchToProps: WrappedMapDispatchToProps, mergeProps: MergeProps, - dispatch: Dispatch, + dispatch: Dispatch>, { areStatesEqual, areOwnPropsEqual, areStatePropsEqual, - }: PureSelectorFactoryComparisonOptions + }: PureSelectorFactoryComparisonOptions ) { let hasRunAtLeastOnce = false let state: State @@ -95,21 +89,17 @@ export function pureFinalPropsSelectorFactory< function handleFirstCall(firstState: State, firstOwnProps: TOwnProps) { state = firstState ownProps = firstOwnProps - // @ts-ignore - stateProps = mapStateToProps!(state, ownProps) - // @ts-ignore - dispatchProps = mapDispatchToProps!(dispatch, ownProps) + stateProps = mapStateToProps(state, ownProps) + dispatchProps = mapDispatchToProps(dispatch, ownProps) mergedProps = mergeProps(stateProps, dispatchProps, ownProps) hasRunAtLeastOnce = true return mergedProps } function handleNewPropsAndNewState() { - // @ts-ignore - stateProps = mapStateToProps!(state, ownProps) + stateProps = mapStateToProps(state, ownProps) - if (mapDispatchToProps!.dependsOnOwnProps) - // @ts-ignore + if (mapDispatchToProps.dependsOnOwnProps) dispatchProps = mapDispatchToProps(dispatch, ownProps) mergedProps = mergeProps(stateProps, dispatchProps, ownProps) @@ -117,12 +107,10 @@ export function pureFinalPropsSelectorFactory< } function handleNewProps() { - if (mapStateToProps!.dependsOnOwnProps) - // @ts-ignore - stateProps = mapStateToProps!(state, ownProps) + if (mapStateToProps.dependsOnOwnProps) + stateProps = mapStateToProps(state, ownProps) if (mapDispatchToProps.dependsOnOwnProps) - // @ts-ignore dispatchProps = mapDispatchToProps(dispatch, ownProps) mergedProps = mergeProps(stateProps, dispatchProps, ownProps) @@ -132,7 +120,6 @@ export function pureFinalPropsSelectorFactory< function handleNewState() { const nextStateProps = mapStateToProps(state, ownProps) const statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps) - // @ts-ignore stateProps = nextStateProps if (statePropsChanged) @@ -163,24 +150,34 @@ export function pureFinalPropsSelectorFactory< } } +interface WrappedMapStateToProps { + (state: State, ownProps: TOwnProps): TStateProps + readonly dependsOnOwnProps: boolean +} + +interface WrappedMapDispatchToProps { + (dispatch: Dispatch>, ownProps: TOwnProps): TDispatchProps + readonly dependsOnOwnProps: boolean +} + export interface SelectorFactoryOptions< TStateProps, TOwnProps, TDispatchProps, TMergedProps, - State = unknown -> extends PureSelectorFactoryComparisonOptions { + State +> extends PureSelectorFactoryComparisonOptions { initMapStateToProps: ( dispatch: Dispatch, - options: PureSelectorFactoryComparisonOptions - ) => MapStateToPropsParam + options: PureSelectorFactoryComparisonOptions + ) => WrappedMapStateToProps initMapDispatchToProps: ( dispatch: Dispatch, - options: PureSelectorFactoryComparisonOptions - ) => MapDispatchToPropsParam + options: PureSelectorFactoryComparisonOptions + ) => WrappedMapDispatchToProps initMergeProps: ( dispatch: Dispatch, - options: PureSelectorFactoryComparisonOptions + options: PureSelectorFactoryComparisonOptions ) => MergeProps } @@ -195,9 +192,9 @@ export default function finalPropsSelectorFactory< TOwnProps, TDispatchProps, TMergedProps, - State = unknown + State >( - dispatch: Dispatch, + dispatch: Dispatch>, { initMapStateToProps, initMapDispatchToProps, @@ -225,6 +222,5 @@ export default function finalPropsSelectorFactory< TDispatchProps, TMergedProps, State - // @ts-ignore - >(mapStateToProps!, mapDispatchToProps, mergeProps, dispatch, options) + >(mapStateToProps, mapDispatchToProps, mergeProps, dispatch, options) } From a652da92d7752241d68e014182e423b6e5fdecc3 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Sun, 10 Apr 2022 18:56:10 -0400 Subject: [PATCH 2/2] Restore optional generic for Selector type --- src/connect/selectorFactory.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/connect/selectorFactory.ts b/src/connect/selectorFactory.ts index cf39e213b..fd5dea5f9 100644 --- a/src/connect/selectorFactory.ts +++ b/src/connect/selectorFactory.ts @@ -7,7 +7,9 @@ export type SelectorFactory = ( factoryOptions: TFactoryOptions ) => Selector -export type Selector = TOwnProps extends null | undefined +export type Selector = TOwnProps extends + | null + | undefined ? (state: S) => TProps : (state: S, ownProps: TOwnProps) => TProps