Skip to content

Commit 19cc9e4

Browse files
committed
Remove ts-ignore for initMergeProps
1 parent c7fdad0 commit 19cc9e4

File tree

6 files changed

+98
-137
lines changed

6 files changed

+98
-137
lines changed

src/components/connect.tsx

+9-47
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* eslint-disable valid-jsdoc, @typescript-eslint/no-unused-vars */
22
import hoistStatics from 'hoist-non-react-statics'
3-
import React, { useContext, useMemo, useRef, useReducer } from 'react'
3+
import React, { useContext, useMemo, useRef } from 'react'
44
import { isValidElementType, isContextConsumer } from 'react-is'
55

6-
import type { Store, Dispatch, Action, AnyAction } from 'redux'
6+
import type { Store } from 'redux'
77

88
import type {
99
AdvancedComponentDecorator,
@@ -21,15 +21,12 @@ import defaultSelectorFactory, {
2121
MapDispatchToPropsNonObject,
2222
SelectorFactoryOptions,
2323
} from '../connect/selectorFactory'
24-
import defaultMapDispatchToPropsFactories from '../connect/mapDispatchToProps'
25-
import defaultMapStateToPropsFactories from '../connect/mapStateToProps'
26-
import defaultMergePropsFactories from '../connect/mergeProps'
24+
import { mapDispatchToPropsFactory } from '../connect/mapDispatchToProps'
25+
import { mapStateToPropsFactory } from '../connect/mapStateToProps'
26+
import { mergePropsFactory } from '../connect/mergeProps'
2727

2828
import { createSubscription, Subscription } from '../utils/Subscription'
29-
import {
30-
useIsomorphicLayoutEffect,
31-
canUseDOM,
32-
} from '../utils/useIsomorphicLayoutEffect'
29+
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
3330
import shallowEqual from '../utils/shallowEqual'
3431

3532
import {
@@ -206,25 +203,6 @@ interface InternalConnectProps extends ConnectProps {
206203
reactReduxForwardedRef?: React.ForwardedRef<unknown>
207204
}
208205

209-
function match<T>(
210-
arg: unknown,
211-
factories: ((value: unknown) => T)[],
212-
name: string
213-
): T {
214-
for (let i = factories.length - 1; i >= 0; i--) {
215-
const result = factories[i](arg)
216-
if (result) return result
217-
}
218-
219-
return ((dispatch: Dispatch, options: { wrappedComponentName: string }) => {
220-
throw new Error(
221-
`Invalid value of type ${typeof arg} for ${name} argument when connecting component ${
222-
options.wrappedComponentName
223-
}.`
224-
)
225-
}) as any
226-
}
227-
228206
function strictEqual(a: unknown, b: unknown) {
229207
return a === b
230208
}
@@ -485,24 +463,9 @@ function connect<
485463

486464
type WrappedComponentProps = TOwnProps & ConnectProps
487465

488-
const initMapStateToProps = match(
489-
mapStateToProps,
490-
// @ts-ignore
491-
defaultMapStateToPropsFactories,
492-
'mapStateToProps'
493-
)!
494-
const initMapDispatchToProps = match(
495-
mapDispatchToProps,
496-
// @ts-ignore
497-
defaultMapDispatchToPropsFactories,
498-
'mapDispatchToProps'
499-
)!
500-
const initMergeProps = match(
501-
mergeProps,
502-
// @ts-ignore
503-
defaultMergePropsFactories,
504-
'mergeProps'
505-
)!
466+
const initMapStateToProps = mapStateToPropsFactory(mapStateToProps)
467+
const initMapDispatchToProps = mapDispatchToPropsFactory(mapDispatchToProps)
468+
const initMergeProps = mergePropsFactory(mergeProps)
506469

507470
const shouldHandleStateChanges = Boolean(mapStateToProps)
508471

@@ -541,7 +504,6 @@ function connect<
541504
initMapStateToProps,
542505
// @ts-ignore
543506
initMapDispatchToProps,
544-
// @ts-ignore
545507
initMergeProps,
546508
areStatesEqual,
547509
areStatePropsEqual,

src/connect/invalidArgFactory.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { Action, Dispatch } from 'redux'
2+
3+
export function createInvalidArgFactory(arg: unknown, name: string) {
4+
return (
5+
dispatch: Dispatch<Action<unknown>>,
6+
options: { readonly wrappedComponentName: string }
7+
) => {
8+
throw new Error(
9+
`Invalid value of type ${typeof arg} for ${name} argument when connecting component ${
10+
options.wrappedComponentName
11+
}.`
12+
)
13+
}
14+
}

src/connect/mapDispatchToProps.ts

+17-28
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
1-
import { ActionCreatorsMapObject, Dispatch } from 'redux'
2-
import { FixTypeLater } from '../types'
1+
import type { Action, Dispatch } from 'redux'
32
import bindActionCreators from '../utils/bindActionCreators'
43
import { wrapMapToPropsConstant, wrapMapToPropsFunc } from './wrapMapToProps'
4+
import { createInvalidArgFactory } from './invalidArgFactory'
5+
import type { MapDispatchToPropsParam } from './selectorFactory'
56

6-
export function whenMapDispatchToPropsIsFunction(
7-
mapDispatchToProps: ActionCreatorsMapObject | FixTypeLater
8-
) {
9-
return typeof mapDispatchToProps === 'function'
10-
? wrapMapToPropsFunc(mapDispatchToProps, 'mapDispatchToProps')
11-
: undefined
12-
}
13-
14-
export function whenMapDispatchToPropsIsMissing(mapDispatchToProps: undefined) {
15-
return !mapDispatchToProps
16-
? wrapMapToPropsConstant((dispatch: Dispatch) => ({
17-
dispatch,
18-
}))
19-
: undefined
20-
}
21-
22-
export function whenMapDispatchToPropsIsObject(
23-
mapDispatchToProps: ActionCreatorsMapObject
7+
export function mapDispatchToPropsFactory<TDispatchProps, TOwnProps>(
8+
mapDispatchToProps:
9+
| MapDispatchToPropsParam<TDispatchProps, TOwnProps>
10+
| undefined
2411
) {
2512
return mapDispatchToProps && typeof mapDispatchToProps === 'object'
26-
? wrapMapToPropsConstant((dispatch: Dispatch) =>
13+
? wrapMapToPropsConstant((dispatch: Dispatch<Action<unknown>>) =>
14+
// @ts-ignore
2715
bindActionCreators(mapDispatchToProps, dispatch)
2816
)
29-
: undefined
17+
: !mapDispatchToProps
18+
? wrapMapToPropsConstant((dispatch: Dispatch<Action<unknown>>) => ({
19+
dispatch,
20+
}))
21+
: typeof mapDispatchToProps === 'function'
22+
? // @ts-ignore
23+
wrapMapToPropsFunc(mapDispatchToProps, 'mapDispatchToProps')
24+
: createInvalidArgFactory(mapDispatchToProps, 'mapDispatchToProps')
3025
}
31-
32-
export default [
33-
whenMapDispatchToPropsIsFunction,
34-
whenMapDispatchToPropsIsMissing,
35-
whenMapDispatchToPropsIsObject,
36-
]

src/connect/mapStateToProps.ts

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import {
2-
MapToProps,
3-
wrapMapToPropsConstant,
4-
wrapMapToPropsFunc,
5-
} from './wrapMapToProps'
1+
import { wrapMapToPropsConstant, wrapMapToPropsFunc } from './wrapMapToProps'
2+
import { createInvalidArgFactory } from './invalidArgFactory'
3+
import type { MapStateToPropsParam } from './selectorFactory'
64

7-
export function whenMapStateToPropsIsFunction(mapStateToProps?: MapToProps) {
8-
return typeof mapStateToProps === 'function'
9-
? wrapMapToPropsFunc(mapStateToProps, 'mapStateToProps')
10-
: undefined
5+
export function mapStateToPropsFactory<TStateProps, TOwnProps, State>(
6+
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>
7+
) {
8+
return !mapStateToProps
9+
? wrapMapToPropsConstant(() => ({}))
10+
: typeof mapStateToProps === 'function'
11+
? // @ts-ignore
12+
wrapMapToPropsFunc(mapStateToProps, 'mapStateToProps')
13+
: createInvalidArgFactory(mapStateToProps, 'mapStateToProps')
1114
}
12-
13-
export function whenMapStateToPropsIsMissing(mapStateToProps?: MapToProps) {
14-
return !mapStateToProps ? wrapMapToPropsConstant(() => ({})) : undefined
15-
}
16-
17-
export default [whenMapStateToPropsIsFunction, whenMapStateToPropsIsMissing]

src/connect/mergeProps.ts

+23-33
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
import { Dispatch } from 'redux'
1+
import type { Action, Dispatch } from 'redux'
22
import verifyPlainObject from '../utils/verifyPlainObject'
3+
import { createInvalidArgFactory } from './invalidArgFactory'
4+
import type { MergeProps } from './selectorFactory'
5+
import type { EqualityFn } from '../types'
36

4-
type MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps> = (
5-
stateProps: TStateProps,
6-
dispatchProps: TDispatchProps,
7-
ownProps: TOwnProps
8-
) => TMergedProps
9-
10-
export function defaultMergeProps<TStateProps, TDispatchProps, TOwnProps>(
7+
export function defaultMergeProps<
8+
TStateProps,
9+
TDispatchProps,
10+
TOwnProps,
11+
TMergedProps
12+
>(
1113
stateProps: TStateProps,
1214
dispatchProps: TDispatchProps,
1315
ownProps: TOwnProps
14-
) {
16+
): TMergedProps {
17+
// @ts-ignore
1518
return { ...ownProps, ...stateProps, ...dispatchProps }
1619
}
1720

18-
interface InitMergeOptions {
19-
displayName: string
20-
areMergedPropsEqual: (a: any, b: any) => boolean
21-
}
22-
2321
export function wrapMergePropsFunc<
2422
TStateProps,
2523
TDispatchProps,
@@ -28,8 +26,11 @@ export function wrapMergePropsFunc<
2826
>(
2927
mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>
3028
): (
31-
dispatch: Dispatch,
32-
options: InitMergeOptions
29+
dispatch: Dispatch<Action<unknown>>,
30+
options: {
31+
readonly displayName: string
32+
readonly areMergedPropsEqual: EqualityFn<TMergedProps>
33+
}
3334
) => MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps> {
3435
return function initMergePropsProxy(
3536
dispatch,
@@ -61,28 +62,17 @@ export function wrapMergePropsFunc<
6162
}
6263
}
6364

64-
export function whenMergePropsIsFunction<
65-
TStateProps,
66-
TDispatchProps,
67-
TOwnProps,
68-
TMergedProps
69-
>(
70-
mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>
71-
) {
72-
return typeof mergeProps === 'function'
73-
? wrapMergePropsFunc(mergeProps)
74-
: undefined
75-
}
76-
77-
export function whenMergePropsIsOmitted<
65+
export function mergePropsFactory<
7866
TStateProps,
7967
TDispatchProps,
8068
TOwnProps,
8169
TMergedProps
8270
>(
8371
mergeProps?: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>
8472
) {
85-
return !mergeProps ? () => defaultMergeProps : undefined
73+
return !mergeProps
74+
? () => defaultMergeProps
75+
: typeof mergeProps === 'function'
76+
? wrapMergePropsFunc(mergeProps)
77+
: createInvalidArgFactory(mergeProps, 'mergeProps')
8678
}
87-
88-
export default [whenMergePropsIsFunction, whenMergePropsIsOmitted] as const

src/connect/selectorFactory.ts

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Dispatch, Action } from 'redux'
2+
import type { ComponentType } from 'react'
23
import verifySubselectors from './verifySubselectors'
34
import type { EqualityFn } from '../types'
45

@@ -58,10 +59,9 @@ export type MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps> = (
5859
) => TMergedProps
5960

6061
interface PureSelectorFactoryComparisonOptions<TStateProps, TOwnProps, State> {
61-
areStatesEqual: EqualityFn<State>
62-
areOwnPropsEqual: EqualityFn<TOwnProps>
63-
areStatePropsEqual: EqualityFn<TStateProps>
64-
displayName: string
62+
readonly areStatesEqual: EqualityFn<State>
63+
readonly areStatePropsEqual: EqualityFn<TStateProps>
64+
readonly areOwnPropsEqual: EqualityFn<TOwnProps>
6565
}
6666

6767
export function pureFinalPropsSelectorFactory<
@@ -162,24 +162,33 @@ interface WrappedMapDispatchToProps<TDispatchProps, TOwnProps> {
162162
readonly dependsOnOwnProps: boolean
163163
}
164164

165+
export interface InitOptions<TStateProps, TOwnProps, TMergedProps, State>
166+
extends PureSelectorFactoryComparisonOptions<TStateProps, TOwnProps, State> {
167+
readonly shouldHandleStateChanges: boolean
168+
readonly displayName: string
169+
readonly wrappedComponentName: string
170+
readonly WrappedComponent: ComponentType<TOwnProps>
171+
readonly areMergedPropsEqual: EqualityFn<TMergedProps>
172+
}
173+
165174
export interface SelectorFactoryOptions<
166175
TStateProps,
167176
TOwnProps,
168177
TDispatchProps,
169178
TMergedProps,
170179
State
171-
> extends PureSelectorFactoryComparisonOptions<TStateProps, TOwnProps, State> {
172-
initMapStateToProps: (
173-
dispatch: Dispatch,
174-
options: PureSelectorFactoryComparisonOptions<TStateProps, TOwnProps, State>
180+
> extends InitOptions<TStateProps, TOwnProps, TMergedProps, State> {
181+
readonly initMapStateToProps: (
182+
dispatch: Dispatch<Action<unknown>>,
183+
options: InitOptions<TStateProps, TOwnProps, TMergedProps, State>
175184
) => WrappedMapStateToProps<TStateProps, TOwnProps, State>
176-
initMapDispatchToProps: (
177-
dispatch: Dispatch,
178-
options: PureSelectorFactoryComparisonOptions<TStateProps, TOwnProps, State>
185+
readonly initMapDispatchToProps: (
186+
dispatch: Dispatch<Action<unknown>>,
187+
options: InitOptions<TStateProps, TOwnProps, TMergedProps, State>
179188
) => WrappedMapDispatchToProps<TDispatchProps, TOwnProps>
180-
initMergeProps: (
181-
dispatch: Dispatch,
182-
options: PureSelectorFactoryComparisonOptions<TStateProps, TOwnProps, State>
189+
readonly initMergeProps: (
190+
dispatch: Dispatch<Action<unknown>>,
191+
options: InitOptions<TStateProps, TOwnProps, TMergedProps, State>
183192
) => MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>
184193
}
185194

0 commit comments

Comments
 (0)