Skip to content

Commit 2debcb5

Browse files
committed
Rework connect types to match current DT typedefs
1 parent dffd200 commit 2debcb5

File tree

4 files changed

+263
-49
lines changed

4 files changed

+263
-49
lines changed

src/components/connectAdvanced.tsx

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import type { Store, AnyAction } from 'redux'
1111
import type { SelectorFactory } from '../connect/selectorFactory'
1212
import { createSubscription, Subscription } from '../utils/Subscription'
1313
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
14-
import type { DispatchProp, Matching, GetProps } from '../types'
14+
import type {
15+
DispatchProp,
16+
Matching,
17+
GetProps,
18+
AdvancedComponentDecorator,
19+
ConnectedComponent,
20+
} from '../types'
1521

1622
import {
1723
ReactReduxContext,
@@ -183,16 +189,7 @@ export interface ConnectAdvancedOptions {
183189
pure?: boolean
184190
}
185191

186-
interface AnyObject {
187-
[x: string]: any
188-
}
189-
190-
export default function connectAdvanced<
191-
S,
192-
TProps,
193-
TOwnProps,
194-
TFactoryOptions extends AnyObject = {}
195-
>(
192+
function connectAdvanced<S, TProps, TOwnProps, TFactoryOptions = {}>(
196193
/*
197194
selectorFactory is a func that is responsible for returning the selector function used to
198195
compute new props from state, props, and dispatch. For example:
@@ -236,12 +233,19 @@ export default function connectAdvanced<
236233
) {
237234
const Context = context
238235

236+
type WrappedComponentProps = TOwnProps & ConnectProps
237+
238+
/*
239239
return function wrapWithConnect<
240-
WC extends React.ComponentClass<
241-
Matching<DispatchProp<AnyAction>, GetProps<WC>>,
242-
any
240+
WC extends React.ComponentType<
241+
Matching<DispatchProp<AnyAction>, GetProps<WC>>
243242
>
244243
>(WrappedComponent: WC) {
244+
*/
245+
const wrapWithConnect: AdvancedComponentDecorator<
246+
TProps,
247+
WrappedComponentProps
248+
> = (WrappedComponent) => {
245249
if (
246250
process.env.NODE_ENV !== 'production' &&
247251
!isValidElementType(WrappedComponent)
@@ -487,7 +491,14 @@ export default function connectAdvanced<
487491
// If we're in "pure" mode, ensure our wrapper component only re-renders when incoming props have changed.
488492
const _Connect = pure ? React.memo(ConnectFunction) : ConnectFunction
489493

490-
const Connect = _Connect as typeof _Connect & { WrappedComponent: WC }
494+
type ConnectedWrapperComponent = typeof _Connect & {
495+
WrappedComponent: typeof WrappedComponent
496+
}
497+
498+
const Connect = _Connect as ConnectedComponent<
499+
typeof WrappedComponent,
500+
WrappedComponentProps
501+
>
491502
Connect.WrappedComponent = WrappedComponent
492503
Connect.displayName = ConnectFunction.displayName = displayName
493504

@@ -496,17 +507,20 @@ export default function connectAdvanced<
496507
props,
497508
ref
498509
) {
510+
// @ts-ignore
499511
return <Connect {...props} reactReduxForwardedRef={ref} />
500512
})
501513

502-
const forwarded = _forwarded as typeof _forwarded & {
503-
WrappedComponent: WC
504-
}
514+
const forwarded = _forwarded as ConnectedWrapperComponent
505515
forwarded.displayName = displayName
506516
forwarded.WrappedComponent = WrappedComponent
507517
return hoistStatics(forwarded, WrappedComponent)
508518
}
509519

510520
return hoistStatics(Connect, WrappedComponent)
511521
}
522+
523+
return wrapWithConnect
512524
}
525+
526+
export default connectAdvanced

0 commit comments

Comments
 (0)