-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Remove ts-ignore for initMergeProps #1891
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
/* eslint-disable valid-jsdoc, @typescript-eslint/no-unused-vars */ | ||
import hoistStatics from 'hoist-non-react-statics' | ||
import React, { useContext, useMemo, useRef, useReducer } from 'react' | ||
import React, { useContext, useMemo, useRef } from 'react' | ||
import { isValidElementType, isContextConsumer } from 'react-is' | ||
|
||
import type { Store, Dispatch, Action, AnyAction } from 'redux' | ||
import type { Store } from 'redux' | ||
|
||
import type { | ||
AdvancedComponentDecorator, | ||
|
@@ -21,15 +21,12 @@ import defaultSelectorFactory, { | |
MapDispatchToPropsNonObject, | ||
SelectorFactoryOptions, | ||
} from '../connect/selectorFactory' | ||
import defaultMapDispatchToPropsFactories from '../connect/mapDispatchToProps' | ||
import defaultMapStateToPropsFactories from '../connect/mapStateToProps' | ||
import defaultMergePropsFactories from '../connect/mergeProps' | ||
import { mapDispatchToPropsFactory } from '../connect/mapDispatchToProps' | ||
import { mapStateToPropsFactory } from '../connect/mapStateToProps' | ||
import { mergePropsFactory } from '../connect/mergeProps' | ||
|
||
import { createSubscription, Subscription } from '../utils/Subscription' | ||
import { | ||
useIsomorphicLayoutEffect, | ||
canUseDOM, | ||
} from '../utils/useIsomorphicLayoutEffect' | ||
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect' | ||
import shallowEqual from '../utils/shallowEqual' | ||
|
||
import { | ||
|
@@ -206,25 +203,6 @@ interface InternalConnectProps extends ConnectProps { | |
reactReduxForwardedRef?: React.ForwardedRef<unknown> | ||
} | ||
|
||
function match<T>( | ||
arg: unknown, | ||
factories: ((value: unknown) => T)[], | ||
name: string | ||
): T { | ||
for (let i = factories.length - 1; i >= 0; i--) { | ||
const result = factories[i](arg) | ||
if (result) return result | ||
} | ||
|
||
return ((dispatch: Dispatch, options: { wrappedComponentName: string }) => { | ||
throw new Error( | ||
`Invalid value of type ${typeof arg} for ${name} argument when connecting component ${ | ||
options.wrappedComponentName | ||
}.` | ||
) | ||
}) as any | ||
} | ||
|
||
function strictEqual(a: unknown, b: unknown) { | ||
return a === b | ||
} | ||
|
@@ -485,24 +463,9 @@ function connect< | |
|
||
type WrappedComponentProps = TOwnProps & ConnectProps | ||
|
||
const initMapStateToProps = match( | ||
mapStateToProps, | ||
// @ts-ignore | ||
defaultMapStateToPropsFactories, | ||
'mapStateToProps' | ||
)! | ||
const initMapDispatchToProps = match( | ||
mapDispatchToProps, | ||
// @ts-ignore | ||
defaultMapDispatchToPropsFactories, | ||
'mapDispatchToProps' | ||
)! | ||
const initMergeProps = match( | ||
mergeProps, | ||
// @ts-ignore | ||
defaultMergePropsFactories, | ||
'mergeProps' | ||
)! | ||
const initMapStateToProps = mapStateToPropsFactory(mapStateToProps) | ||
const initMapDispatchToProps = mapDispatchToPropsFactory(mapDispatchToProps) | ||
const initMergeProps = mergePropsFactory(mergeProps) | ||
|
||
const shouldHandleStateChanges = Boolean(mapStateToProps) | ||
|
||
|
@@ -541,7 +504,6 @@ function connect< | |
initMapStateToProps, | ||
// @ts-ignore | ||
initMapDispatchToProps, | ||
// @ts-ignore | ||
initMergeProps, | ||
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. The goal of this PR was to remove this |
||
areStatesEqual, | ||
areStatePropsEqual, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { Action, Dispatch } from 'redux' | ||
|
||
export function createInvalidArgFactory(arg: unknown, name: string) { | ||
return ( | ||
dispatch: Dispatch<Action<unknown>>, | ||
options: { readonly wrappedComponentName: string } | ||
) => { | ||
throw new Error( | ||
`Invalid value of type ${typeof arg} for ${name} argument when connecting component ${ | ||
options.wrappedComponentName | ||
}.` | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,25 @@ | ||
import { ActionCreatorsMapObject, Dispatch } from 'redux' | ||
import { FixTypeLater } from '../types' | ||
import type { Action, Dispatch } from 'redux' | ||
import bindActionCreators from '../utils/bindActionCreators' | ||
import { wrapMapToPropsConstant, wrapMapToPropsFunc } from './wrapMapToProps' | ||
import { createInvalidArgFactory } from './invalidArgFactory' | ||
import type { MapDispatchToPropsParam } from './selectorFactory' | ||
|
||
export function whenMapDispatchToPropsIsFunction( | ||
mapDispatchToProps: ActionCreatorsMapObject | FixTypeLater | ||
) { | ||
return typeof mapDispatchToProps === 'function' | ||
? wrapMapToPropsFunc(mapDispatchToProps, 'mapDispatchToProps') | ||
: undefined | ||
} | ||
|
||
export function whenMapDispatchToPropsIsMissing(mapDispatchToProps: undefined) { | ||
return !mapDispatchToProps | ||
? wrapMapToPropsConstant((dispatch: Dispatch) => ({ | ||
dispatch, | ||
})) | ||
: undefined | ||
} | ||
|
||
export function whenMapDispatchToPropsIsObject( | ||
mapDispatchToProps: ActionCreatorsMapObject | ||
export function mapDispatchToPropsFactory<TDispatchProps, TOwnProps>( | ||
mapDispatchToProps: | ||
| MapDispatchToPropsParam<TDispatchProps, TOwnProps> | ||
| undefined | ||
) { | ||
return mapDispatchToProps && typeof mapDispatchToProps === 'object' | ||
? wrapMapToPropsConstant((dispatch: Dispatch) => | ||
? wrapMapToPropsConstant((dispatch: Dispatch<Action<unknown>>) => | ||
// @ts-ignore | ||
bindActionCreators(mapDispatchToProps, dispatch) | ||
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. To be fixed in a later PR. |
||
) | ||
: undefined | ||
: !mapDispatchToProps | ||
? wrapMapToPropsConstant((dispatch: Dispatch<Action<unknown>>) => ({ | ||
dispatch, | ||
})) | ||
: typeof mapDispatchToProps === 'function' | ||
? // @ts-ignore | ||
wrapMapToPropsFunc(mapDispatchToProps, 'mapDispatchToProps') | ||
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. To be fixed in a later PR. |
||
: createInvalidArgFactory(mapDispatchToProps, 'mapDispatchToProps') | ||
} | ||
|
||
export default [ | ||
whenMapDispatchToPropsIsFunction, | ||
whenMapDispatchToPropsIsMissing, | ||
whenMapDispatchToPropsIsObject, | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,14 @@ | ||
import { | ||
MapToProps, | ||
wrapMapToPropsConstant, | ||
wrapMapToPropsFunc, | ||
} from './wrapMapToProps' | ||
import { wrapMapToPropsConstant, wrapMapToPropsFunc } from './wrapMapToProps' | ||
import { createInvalidArgFactory } from './invalidArgFactory' | ||
import type { MapStateToPropsParam } from './selectorFactory' | ||
|
||
export function whenMapStateToPropsIsFunction(mapStateToProps?: MapToProps) { | ||
return typeof mapStateToProps === 'function' | ||
? wrapMapToPropsFunc(mapStateToProps, 'mapStateToProps') | ||
: undefined | ||
export function mapStateToPropsFactory<TStateProps, TOwnProps, State>( | ||
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State> | ||
) { | ||
return !mapStateToProps | ||
? wrapMapToPropsConstant(() => ({})) | ||
: typeof mapStateToProps === 'function' | ||
? // @ts-ignore | ||
wrapMapToPropsFunc(mapStateToProps, 'mapStateToProps') | ||
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. To be fixed in a later PR. |
||
: createInvalidArgFactory(mapStateToProps, 'mapStateToProps') | ||
} | ||
|
||
export function whenMapStateToPropsIsMissing(mapStateToProps?: MapToProps) { | ||
return !mapStateToProps ? wrapMapToPropsConstant(() => ({})) : undefined | ||
} | ||
|
||
export default [whenMapStateToPropsIsFunction, whenMapStateToPropsIsMissing] |
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.
The
match
function is not TypeScript friendly. I refactored those to just be functions (that I think are just as easy to understand as before).