Skip to content

Commit 0cece74

Browse files
authored
chore(mapStateToProps): port to typescript (#1745)
1 parent 7dcd152 commit 0cece74

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

src/connect/mapStateToProps.js renamed to src/connect/mapStateToProps.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { wrapMapToPropsConstant, wrapMapToPropsFunc } from './wrapMapToProps'
1+
import {
2+
MapToProps,
3+
wrapMapToPropsConstant,
4+
wrapMapToPropsFunc,
5+
} from './wrapMapToProps'
26

3-
export function whenMapStateToPropsIsFunction(mapStateToProps) {
7+
export function whenMapStateToPropsIsFunction(mapStateToProps?: MapToProps) {
48
return typeof mapStateToProps === 'function'
59
? wrapMapToPropsFunc(mapStateToProps, 'mapStateToProps')
610
: undefined
711
}
812

9-
export function whenMapStateToPropsIsMissing(mapStateToProps) {
13+
export function whenMapStateToPropsIsMissing(mapStateToProps?: MapToProps) {
1014
return !mapStateToProps ? wrapMapToPropsConstant(() => ({})) : undefined
1115
}
1216

src/connect/wrapMapToProps.js renamed to src/connect/wrapMapToProps.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1+
import { Dispatch } from 'redux'
2+
3+
import { FixTypeLater } from '../types'
14
import 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

Comments
 (0)