-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Cleanup internal types in selectorFactory.ts #1889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ import verifySubselectors from './verifySubselectors' | |
import type { EqualityFn } from '../types' | ||
|
||
export type SelectorFactory<S, TProps, TOwnProps, TFactoryOptions> = ( | ||
dispatch: Dispatch<Action>, | ||
dispatch: Dispatch<Action<unknown>>, | ||
factoryOptions: TFactoryOptions | ||
) => Selector<S, TProps, TOwnProps> | ||
|
||
|
@@ -13,24 +13,24 @@ export type Selector<S, TProps, TOwnProps = null> = TOwnProps extends | |
? (state: S) => TProps | ||
: (state: S, ownProps: TOwnProps) => TProps | ||
|
||
export type MapStateToProps<TStateProps, TOwnProps, State = unknown> = ( | ||
export type MapStateToProps<TStateProps, TOwnProps, State> = ( | ||
state: State, | ||
ownProps: TOwnProps | ||
) => TStateProps | ||
|
||
export type MapStateToPropsFactory<TStateProps, TOwnProps, State = unknown> = ( | ||
export type MapStateToPropsFactory<TStateProps, TOwnProps, State> = ( | ||
initialState: State, | ||
ownProps: TOwnProps | ||
) => MapStateToProps<TStateProps, TOwnProps, State> | ||
|
||
export type MapStateToPropsParam<TStateProps, TOwnProps, State = unknown> = | ||
export type MapStateToPropsParam<TStateProps, TOwnProps, State> = | ||
| MapStateToPropsFactory<TStateProps, TOwnProps, State> | ||
| MapStateToProps<TStateProps, TOwnProps, State> | ||
| null | ||
| undefined | ||
|
||
export type MapDispatchToPropsFunction<TDispatchProps, TOwnProps> = ( | ||
dispatch: Dispatch<Action>, | ||
dispatch: Dispatch<Action<unknown>>, | ||
ownProps: TOwnProps | ||
) => TDispatchProps | ||
|
||
|
@@ -39,7 +39,7 @@ export type MapDispatchToProps<TDispatchProps, TOwnProps> = | |
| TDispatchProps | ||
|
||
export type MapDispatchToPropsFactory<TDispatchProps, TOwnProps> = ( | ||
dispatch: Dispatch<Action>, | ||
dispatch: Dispatch<Action<unknown>>, | ||
ownProps: TOwnProps | ||
) => MapDispatchToPropsFunction<TDispatchProps, TOwnProps> | ||
|
||
|
@@ -57,10 +57,10 @@ export type MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps> = ( | |
ownProps: TOwnProps | ||
) => TMergedProps | ||
|
||
interface PureSelectorFactoryComparisonOptions<TOwnProps, State = unknown> { | ||
interface PureSelectorFactoryComparisonOptions<TStateProps, TOwnProps, State> { | ||
areStatesEqual: EqualityFn<State> | ||
areOwnPropsEqual: EqualityFn<TOwnProps> | ||
areStatePropsEqual: EqualityFn<unknown> | ||
areStatePropsEqual: EqualityFn<TStateProps> | ||
displayName: string | ||
} | ||
|
||
|
@@ -69,21 +69,17 @@ export function pureFinalPropsSelectorFactory< | |
TOwnProps, | ||
TDispatchProps, | ||
TMergedProps, | ||
State = unknown | ||
State | ||
>( | ||
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State> & { | ||
dependsOnOwnProps: boolean | ||
}, | ||
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps> & { | ||
dependsOnOwnProps: boolean | ||
}, | ||
mapStateToProps: WrappedMapStateToProps<TStateProps, TOwnProps, State>, | ||
mapDispatchToProps: WrappedMapDispatchToProps<TDispatchProps, TOwnProps>, | ||
mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>, | ||
dispatch: Dispatch, | ||
dispatch: Dispatch<Action<unknown>>, | ||
{ | ||
areStatesEqual, | ||
areOwnPropsEqual, | ||
areStatePropsEqual, | ||
}: PureSelectorFactoryComparisonOptions<TOwnProps, State> | ||
}: PureSelectorFactoryComparisonOptions<TStateProps, TOwnProps, State> | ||
) { | ||
let hasRunAtLeastOnce = false | ||
let state: State | ||
|
@@ -95,34 +91,28 @@ 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) | ||
return mergedProps | ||
} | ||
|
||
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 +122,6 @@ export function pureFinalPropsSelectorFactory< | |
function handleNewState() { | ||
const nextStateProps = mapStateToProps(state, ownProps) | ||
const statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps) | ||
// @ts-ignore | ||
stateProps = nextStateProps | ||
|
||
if (statePropsChanged) | ||
|
@@ -163,24 +152,34 @@ export function pureFinalPropsSelectorFactory< | |
} | ||
} | ||
|
||
interface WrappedMapStateToProps<TStateProps, TOwnProps, State> { | ||
(state: State, ownProps: TOwnProps): TStateProps | ||
readonly dependsOnOwnProps: boolean | ||
} | ||
|
||
interface WrappedMapDispatchToProps<TDispatchProps, TOwnProps> { | ||
(dispatch: Dispatch<Action<unknown>>, ownProps: TOwnProps): TDispatchProps | ||
readonly dependsOnOwnProps: boolean | ||
} | ||
|
||
export interface SelectorFactoryOptions< | ||
TStateProps, | ||
TOwnProps, | ||
TDispatchProps, | ||
TMergedProps, | ||
State = unknown | ||
> extends PureSelectorFactoryComparisonOptions<TOwnProps, State> { | ||
State | ||
> extends PureSelectorFactoryComparisonOptions<TStateProps, TOwnProps, State> { | ||
initMapStateToProps: ( | ||
dispatch: Dispatch, | ||
options: PureSelectorFactoryComparisonOptions<TOwnProps, State> | ||
) => MapStateToPropsParam<TStateProps, TOwnProps, State> | ||
options: PureSelectorFactoryComparisonOptions<TStateProps, TOwnProps, State> | ||
) => WrappedMapStateToProps<TStateProps, TOwnProps, State> | ||
initMapDispatchToProps: ( | ||
dispatch: Dispatch, | ||
options: PureSelectorFactoryComparisonOptions<TOwnProps, State> | ||
) => MapDispatchToPropsParam<TDispatchProps, TOwnProps> | ||
options: PureSelectorFactoryComparisonOptions<TStateProps, TOwnProps, State> | ||
) => WrappedMapDispatchToProps<TDispatchProps, TOwnProps> | ||
Comment on lines
172
to
+179
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the main change. |
||
initMergeProps: ( | ||
dispatch: Dispatch, | ||
options: PureSelectorFactoryComparisonOptions<TOwnProps, State> | ||
options: PureSelectorFactoryComparisonOptions<TStateProps, TOwnProps, State> | ||
) => MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps> | ||
} | ||
|
||
|
@@ -195,9 +194,9 @@ export default function finalPropsSelectorFactory< | |
TOwnProps, | ||
TDispatchProps, | ||
TMergedProps, | ||
State = unknown | ||
State | ||
>( | ||
dispatch: Dispatch<Action>, | ||
dispatch: Dispatch<Action<unknown>>, | ||
{ | ||
initMapStateToProps, | ||
initMapDispatchToProps, | ||
|
@@ -225,6 +224,5 @@ export default function finalPropsSelectorFactory< | |
TDispatchProps, | ||
TMergedProps, | ||
State | ||
// @ts-ignore | ||
>(mapStateToProps!, mapDispatchToProps, mergeProps, dispatch, options) | ||
>(mapStateToProps, mapDispatchToProps, mergeProps, dispatch, options) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly here, narrowing the argument type fixed most of the type errors.