1+ import { Dispatch } from 'redux'
2+
3+ import { FixTypeLater } from '../types'
14import verifyPlainObject from '../utils/verifyPlainObject'
25
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 )
626
727 function constantSelector ( ) {
828 return constant
@@ -19,9 +39,8 @@ export function wrapMapToPropsConstant(getConstant) {
1939// A length of one signals that mapToProps does not depend on props from the parent component.
2040// A length of zero is assumed to mean mapToProps is getting args via arguments or ...args and
2141// 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
2544 ? Boolean ( mapToProps . dependsOnOwnProps )
2645 : mapToProps . length !== 1
2746}
@@ -38,21 +57,30 @@ export function getDependsOnOwnProps(mapToProps) {
3857// * On first call, verifies the first result is a plain object, in order to warn
3958// the developer that their mapToProps function is not returning a valid result.
4059//
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 {
4472 return proxy . dependsOnOwnProps
4573 ? proxy . mapToProps ( stateOrDispatch , ownProps )
46- : proxy . mapToProps ( stateOrDispatch )
74+ : proxy . mapToProps ( stateOrDispatch , undefined )
4775 }
4876
4977 // allow detectFactoryAndVerify to get ownProps
5078 proxy . dependsOnOwnProps = true
5179
5280 proxy . mapToProps = function detectFactoryAndVerify (
53- stateOrDispatch ,
54- ownProps
55- ) {
81+ stateOrDispatch : StateOrDispatch ,
82+ ownProps ?: P
83+ ) : MapToProps {
5684 proxy . mapToProps = mapToProps
5785 proxy . dependsOnOwnProps = getDependsOnOwnProps ( mapToProps )
5886 let props = proxy ( stateOrDispatch , ownProps )
0 commit comments