@@ -3,18 +3,23 @@ import { Dispatch } from 'redux'
3
3
import { FixTypeLater } from '../types'
4
4
import verifyPlainObject from '../utils/verifyPlainObject'
5
5
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
8
13
dependsOnOwnProps : boolean
9
14
}
10
15
11
16
export function wrapMapToPropsConstant (
12
17
// * 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)
16
21
//
17
- getConstant : ( dispatch : Dispatch ) => FixTypeLater
22
+ getConstant : ( dispatch : Dispatch ) => { dispatch : Dispatch }
18
23
) {
19
24
return function initConstantSelector ( dispatch : Dispatch ) {
20
25
const constant = getConstant ( dispatch )
@@ -52,26 +57,29 @@ export function getDependsOnOwnProps(mapToProps: MapToProps) {
52
57
// * On first call, verifies the first result is a plain object, in order to warn
53
58
// the developer that their mapToProps function is not returning a valid result.
54
59
//
55
- export function wrapMapToPropsFunc ( mapToProps : MapToProps , methodName : string ) {
60
+ export function wrapMapToPropsFunc < P = AnyProps > (
61
+ mapToProps : MapToProps ,
62
+ methodName : string
63
+ ) {
56
64
return function initProxySelector (
57
65
dispatch : Dispatch ,
58
66
{ displayName } : { displayName : string }
59
67
) {
60
68
const proxy = function mapToPropsProxy (
61
69
stateOrDispatch : FixTypeLater ,
62
- ownProps : unknown
70
+ ownProps ?: P
63
71
) : FixTypeLater {
64
72
return proxy . dependsOnOwnProps
65
73
? proxy . mapToProps ( stateOrDispatch , ownProps )
66
- : proxy . mapToProps ( stateOrDispatch , null )
74
+ : proxy . mapToProps ( stateOrDispatch , undefined )
67
75
}
68
76
69
77
// allow detectFactoryAndVerify to get ownProps
70
78
proxy . dependsOnOwnProps = true
71
79
72
80
proxy . mapToProps = function detectFactoryAndVerify (
73
81
stateOrDispatch : FixTypeLater ,
74
- ownProps : unknown
82
+ ownProps ?: P
75
83
) {
76
84
proxy . mapToProps = mapToProps
77
85
proxy . dependsOnOwnProps = getDependsOnOwnProps ( mapToProps )
0 commit comments