1
+ import { Dispatch } from 'redux'
2
+
3
+ import { FixTypeLater } from '../types'
1
4
import verifyPlainObject from '../utils/verifyPlainObject'
2
5
3
- export function wrapMapToPropsConstant ( getConstant ) {
4
- return function initConstantSelector ( dispatch , options ) {
5
- const constant = getConstant ( dispatch , options )
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
14
+ }
15
+
16
+ export function wrapMapToPropsConstant (
17
+ // * Note:
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)
21
+ //
22
+ getConstant : ( dispatch : Dispatch ) => { dispatch ?: Dispatch }
23
+ ) {
24
+ return function initConstantSelector ( dispatch : Dispatch ) {
25
+ const constant = getConstant ( dispatch )
6
26
7
27
function constantSelector ( ) {
8
28
return constant
@@ -19,9 +39,8 @@ export function wrapMapToPropsConstant(getConstant) {
19
39
// A length of one signals that mapToProps does not depend on props from the parent component.
20
40
// A length of zero is assumed to mean mapToProps is getting args via arguments or ...args and
21
41
// therefore not reporting its length accurately..
22
- export function getDependsOnOwnProps ( mapToProps ) {
23
- return mapToProps . dependsOnOwnProps !== null &&
24
- mapToProps . dependsOnOwnProps !== undefined
42
+ export function getDependsOnOwnProps ( mapToProps : MapToProps ) {
43
+ return mapToProps ?. dependsOnOwnProps
25
44
? Boolean ( mapToProps . dependsOnOwnProps )
26
45
: mapToProps . length !== 1
27
46
}
@@ -38,21 +57,30 @@ export function getDependsOnOwnProps(mapToProps) {
38
57
// * On first call, verifies the first result is a plain object, in order to warn
39
58
// the developer that their mapToProps function is not returning a valid result.
40
59
//
41
- export function wrapMapToPropsFunc ( mapToProps , methodName ) {
42
- return function initProxySelector ( dispatch , { displayName } ) {
43
- const proxy = function mapToPropsProxy ( stateOrDispatch , ownProps ) {
60
+ export function wrapMapToPropsFunc < P = AnyProps > (
61
+ mapToProps : MapToProps ,
62
+ methodName : string
63
+ ) {
64
+ return function initProxySelector (
65
+ dispatch : Dispatch ,
66
+ { displayName } : { displayName : string }
67
+ ) {
68
+ const proxy = function mapToPropsProxy (
69
+ stateOrDispatch : StateOrDispatch ,
70
+ ownProps ?: P
71
+ ) : MapToProps {
44
72
return proxy . dependsOnOwnProps
45
73
? proxy . mapToProps ( stateOrDispatch , ownProps )
46
- : proxy . mapToProps ( stateOrDispatch )
74
+ : proxy . mapToProps ( stateOrDispatch , undefined )
47
75
}
48
76
49
77
// allow detectFactoryAndVerify to get ownProps
50
78
proxy . dependsOnOwnProps = true
51
79
52
80
proxy . mapToProps = function detectFactoryAndVerify (
53
- stateOrDispatch ,
54
- ownProps
55
- ) {
81
+ stateOrDispatch : StateOrDispatch ,
82
+ ownProps ?: P
83
+ ) : MapToProps {
56
84
proxy . mapToProps = mapToProps
57
85
proxy . dependsOnOwnProps = getDependsOnOwnProps ( mapToProps )
58
86
let props = proxy ( stateOrDispatch , ownProps )
0 commit comments