Skip to content

Commit 9c7ca0e

Browse files
tony-gotimdorr
authored andcommitted
chore(mapStateToProps): move to ts first pass
1 parent 76ed07f commit 9c7ca0e

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

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

+7-3
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

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
import { Dispatch } from 'redux'
2+
13
import verifyPlainObject from '../utils/verifyPlainObject'
24

3-
export function wrapMapToPropsConstant(getConstant) {
4-
return function initConstantSelector(dispatch, options) {
5+
export type MapToProps = {
6+
(stateOrDispatch: any, ownProps?: any): any
7+
dependsOnOwnProps: boolean
8+
}
9+
10+
export function wrapMapToPropsConstant(
11+
getConstant: (dispatch: Dispatch, options: any) => void
12+
) {
13+
return function initConstantSelector(dispatch: Dispatch, options: any) {
514
const constant = getConstant(dispatch, options)
615

716
function constantSelector() {
@@ -19,9 +28,8 @@ export function wrapMapToPropsConstant(getConstant) {
1928
// A length of one signals that mapToProps does not depend on props from the parent component.
2029
// A length of zero is assumed to mean mapToProps is getting args via arguments or ...args and
2130
// therefore not reporting its length accurately..
22-
export function getDependsOnOwnProps(mapToProps) {
23-
return mapToProps.dependsOnOwnProps !== null &&
24-
mapToProps.dependsOnOwnProps !== undefined
31+
export function getDependsOnOwnProps(mapToProps: MapToProps) {
32+
return mapToProps?.dependsOnOwnProps
2533
? Boolean(mapToProps.dependsOnOwnProps)
2634
: mapToProps.length !== 1
2735
}
@@ -38,20 +46,26 @@ export function getDependsOnOwnProps(mapToProps) {
3846
// * On first call, verifies the first result is a plain object, in order to warn
3947
// the developer that their mapToProps function is not returning a valid result.
4048
//
41-
export function wrapMapToPropsFunc(mapToProps, methodName) {
42-
return function initProxySelector(dispatch, { displayName }) {
43-
const proxy = function mapToPropsProxy(stateOrDispatch, ownProps) {
49+
export function wrapMapToPropsFunc(mapToProps: MapToProps, methodName: string) {
50+
return function initProxySelector(
51+
dispatch: Dispatch,
52+
{ displayName }: { displayName: string }
53+
) {
54+
const proxy = function mapToPropsProxy(
55+
stateOrDispatch: any,
56+
ownProps: any
57+
): any {
4458
return proxy.dependsOnOwnProps
4559
? proxy.mapToProps(stateOrDispatch, ownProps)
46-
: proxy.mapToProps(stateOrDispatch)
60+
: proxy.mapToProps(stateOrDispatch, null)
4761
}
4862

4963
// allow detectFactoryAndVerify to get ownProps
5064
proxy.dependsOnOwnProps = true
5165

5266
proxy.mapToProps = function detectFactoryAndVerify(
53-
stateOrDispatch,
54-
ownProps
67+
stateOrDispatch: any,
68+
ownProps: any
5569
) {
5670
proxy.mapToProps = mapToProps
5771
proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps)

0 commit comments

Comments
 (0)