Skip to content

Commit 4137320

Browse files
committed
feat: enrich types
1 parent bc1c647 commit 4137320

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/connect/wrapMapToProps.ts

+23-15
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ import { Dispatch } from 'redux'
33
import { FixTypeLater } from '../types'
44
import verifyPlainObject from '../utils/verifyPlainObject'
55

6-
export type MapToProps = {
7-
(stateOrDispatch: FixTypeLater, ownProps?: unknown): FixTypeLater
8-
dependsOnOwnProps: boolean
6+
type AnyState = { [key: string]: any }
7+
type StateOrDispatch<S = AnyState> = S | Dispatch
8+
9+
type AnyProps = { [key: string]: any }
10+
11+
export type MapToProps<P = AnyProps> = {
12+
(stateOrDispatch: StateOrDispatch, ownProps?: P): FixTypeLater
13+
dependsOnOwnProps?: boolean
914
}
1015

1116
export function wrapMapToPropsConstant(
1217
// * Note:
13-
// It seems that the dispatch identifier here
14-
// could be dispatch in some cases (ex: whenMapDispatchToPropsIsMissing)
15-
// and state in some others (ex: whenMapStateToPropsIsMissing)
18+
// It seems that the dispatch argument
19+
// could be a dispatch function in some cases (ex: whenMapDispatchToPropsIsMissing)
20+
// and a state object in some others (ex: whenMapStateToPropsIsMissing)
1621
//
17-
getConstant: (dispatch: Dispatch) => FixTypeLater
22+
getConstant: (dispatch: Dispatch) => { dispatch?: Dispatch }
1823
) {
1924
return function initConstantSelector(dispatch: Dispatch) {
2025
const constant = getConstant(dispatch)
@@ -52,27 +57,30 @@ export function getDependsOnOwnProps(mapToProps: MapToProps) {
5257
// * On first call, verifies the first result is a plain object, in order to warn
5358
// the developer that their mapToProps function is not returning a valid result.
5459
//
55-
export function wrapMapToPropsFunc(mapToProps: MapToProps, methodName: string) {
60+
export function wrapMapToPropsFunc<P = AnyProps>(
61+
mapToProps: MapToProps,
62+
methodName: string
63+
) {
5664
return function initProxySelector(
5765
dispatch: Dispatch,
5866
{ displayName }: { displayName: string }
5967
) {
6068
const proxy = function mapToPropsProxy(
61-
stateOrDispatch: FixTypeLater,
62-
ownProps: unknown
63-
): FixTypeLater {
69+
stateOrDispatch: StateOrDispatch,
70+
ownProps?: P
71+
): MapToProps {
6472
return proxy.dependsOnOwnProps
6573
? proxy.mapToProps(stateOrDispatch, ownProps)
66-
: proxy.mapToProps(stateOrDispatch, null)
74+
: proxy.mapToProps(stateOrDispatch, undefined)
6775
}
6876

6977
// allow detectFactoryAndVerify to get ownProps
7078
proxy.dependsOnOwnProps = true
7179

7280
proxy.mapToProps = function detectFactoryAndVerify(
73-
stateOrDispatch: FixTypeLater,
74-
ownProps: unknown
75-
) {
81+
stateOrDispatch: StateOrDispatch,
82+
ownProps?: P
83+
): MapToProps {
7684
proxy.mapToProps = mapToProps
7785
proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps)
7886
let props = proxy(stateOrDispatch, ownProps)

test/components/connect.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2660,7 +2660,7 @@ describe('React', () => {
26602660
store.dispatch({ type: 'test' })
26612661
})
26622662

2663-
expect(initialOwnProps).toBe(null)
2663+
expect(initialOwnProps).toBe(undefined)
26642664
expect(initialState).not.toBe(undefined)
26652665
expect(secondaryOwnProps).not.toBe(undefined)
26662666
expect(secondaryOwnProps.name).toBe('a')

0 commit comments

Comments
 (0)