Skip to content

Commit 0fad9ff

Browse files
committed
wip
1 parent bc1c647 commit 0fad9ff

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/connect/wrapMapToProps.ts

+18-10
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
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
813
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,26 +57,29 @@ 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(
6169
stateOrDispatch: FixTypeLater,
62-
ownProps: unknown
70+
ownProps?: P
6371
): FixTypeLater {
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(
7381
stateOrDispatch: FixTypeLater,
74-
ownProps: unknown
82+
ownProps?: P
7583
) {
7684
proxy.mapToProps = mapToProps
7785
proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps)

test/components/connect.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2626,7 +2626,7 @@ describe('React', () => {
26262626
expect(memoizedReturnCount).toBe(2)
26272627
})
26282628

2629-
it('should allow a mapStateToProps factory consuming just state to return a function that gets ownProps', () => {
2629+
it.only('should allow a mapStateToProps factory consuming just state to return a function that gets ownProps', () => {
26302630
const store = createStore(() => ({ value: 1 }))
26312631

26322632
let initialState
@@ -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)