Skip to content

Commit 147f178

Browse files
authored
chore(mapDispatchToProps): port to typescript (#1760)
* chore(mapDispatchToProps): port to typescript * chore: resolve conflicts * fix: types * fix: add FixTypeLater
1 parent b8a118a commit 147f178

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

Diff for: src/connect/mapDispatchToProps.js renamed to src/connect/mapDispatchToProps.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1+
import { ActionCreatorsMapObject, Dispatch } from 'redux'
2+
import { FixTypeLater } from '../types'
13
import bindActionCreators from '../utils/bindActionCreators'
24
import { wrapMapToPropsConstant, wrapMapToPropsFunc } from './wrapMapToProps'
35

4-
export function whenMapDispatchToPropsIsFunction(mapDispatchToProps) {
6+
export function whenMapDispatchToPropsIsFunction(
7+
mapDispatchToProps: ActionCreatorsMapObject | FixTypeLater
8+
) {
59
return typeof mapDispatchToProps === 'function'
610
? wrapMapToPropsFunc(mapDispatchToProps, 'mapDispatchToProps')
711
: undefined
812
}
913

10-
export function whenMapDispatchToPropsIsMissing(mapDispatchToProps) {
14+
export function whenMapDispatchToPropsIsMissing(mapDispatchToProps: undefined) {
1115
return !mapDispatchToProps
12-
? wrapMapToPropsConstant((dispatch) => ({ dispatch }))
16+
? wrapMapToPropsConstant((dispatch: Dispatch) => ({
17+
dispatch,
18+
}))
1319
: undefined
1420
}
1521

16-
export function whenMapDispatchToPropsIsObject(mapDispatchToProps) {
22+
export function whenMapDispatchToPropsIsObject(
23+
mapDispatchToProps: ActionCreatorsMapObject
24+
) {
1725
return mapDispatchToProps && typeof mapDispatchToProps === 'object'
18-
? wrapMapToPropsConstant((dispatch) =>
26+
? wrapMapToPropsConstant((dispatch: Dispatch) =>
1927
bindActionCreators(mapDispatchToProps, dispatch)
2028
)
2129
: undefined

Diff for: src/connect/wrapMapToProps.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Dispatch } from 'redux'
1+
import { ActionCreatorsMapObject, Dispatch, ActionCreator } from 'redux'
22

33
import { FixTypeLater } from '../types'
44
import verifyPlainObject from '../utils/verifyPlainObject'
@@ -20,7 +20,13 @@ export function wrapMapToPropsConstant(
2020
// could be a dispatch function in some cases (ex: whenMapDispatchToPropsIsMissing)
2121
// and a state object in some others (ex: whenMapStateToPropsIsMissing)
2222
// eslint-disable-next-line no-unused-vars
23-
getConstant: (dispatch: Dispatch) => { dispatch?: Dispatch }
23+
getConstant: (dispatch: Dispatch) =>
24+
| {
25+
dispatch?: Dispatch
26+
dependsOnOwnProps?: boolean
27+
}
28+
| ActionCreatorsMapObject
29+
| ActionCreator<any>
2430
) {
2531
return function initConstantSelector(dispatch: Dispatch) {
2632
const constant = getConstant(dispatch)

Diff for: src/utils/bindActionCreators.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { ActionCreatorsMapObject, Dispatch } from 'redux'
33
export default function bindActionCreators(
44
actionCreators: ActionCreatorsMapObject,
55
dispatch: Dispatch
6-
) {
7-
const boundActionCreators: ActionCreatorsMapObject<any> = {}
6+
): ActionCreatorsMapObject {
7+
const boundActionCreators: ActionCreatorsMapObject = {}
8+
89
for (const key in actionCreators) {
910
const actionCreator = actionCreators[key]
1011
if (typeof actionCreator === 'function') {

0 commit comments

Comments
 (0)