Skip to content

chore(mapDispatchToProps): port to typescript #1760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { ActionCreatorsMapObject, Dispatch } from 'redux'
import { FixTypeLater } from '../types'
import bindActionCreators from '../utils/bindActionCreators'
import { wrapMapToPropsConstant, wrapMapToPropsFunc } from './wrapMapToProps'

export function whenMapDispatchToPropsIsFunction(mapDispatchToProps) {
export function whenMapDispatchToPropsIsFunction(
mapDispatchToProps: ActionCreatorsMapObject | FixTypeLater
) {
return typeof mapDispatchToProps === 'function'
? wrapMapToPropsFunc(mapDispatchToProps, 'mapDispatchToProps')
: undefined
}

export function whenMapDispatchToPropsIsMissing(mapDispatchToProps) {
export function whenMapDispatchToPropsIsMissing(mapDispatchToProps: undefined) {
return !mapDispatchToProps
? wrapMapToPropsConstant((dispatch) => ({ dispatch }))
? wrapMapToPropsConstant((dispatch: Dispatch) => ({
dispatch,
}))
: undefined
}

export function whenMapDispatchToPropsIsObject(mapDispatchToProps) {
export function whenMapDispatchToPropsIsObject(
mapDispatchToProps: ActionCreatorsMapObject
) {
return mapDispatchToProps && typeof mapDispatchToProps === 'object'
? wrapMapToPropsConstant((dispatch) =>
? wrapMapToPropsConstant((dispatch: Dispatch) =>
bindActionCreators(mapDispatchToProps, dispatch)
)
: undefined
Expand Down
10 changes: 8 additions & 2 deletions src/connect/wrapMapToProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dispatch } from 'redux'
import { ActionCreatorsMapObject, Dispatch, ActionCreator } from 'redux'

import { FixTypeLater } from '../types'
import verifyPlainObject from '../utils/verifyPlainObject'
Expand All @@ -20,7 +20,13 @@ export function wrapMapToPropsConstant(
// could be a dispatch function in some cases (ex: whenMapDispatchToPropsIsMissing)
// and a state object in some others (ex: whenMapStateToPropsIsMissing)
// eslint-disable-next-line no-unused-vars
getConstant: (dispatch: Dispatch) => { dispatch?: Dispatch }
getConstant: (dispatch: Dispatch) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✋ This should probably be reworked to be more generic and flexible - maybe like:

getConstant: <T>(value: T) => ( () => T)

In other words, whatever you pass in, it returns a function that returns that thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't match with the usage of wrapMapToPropsConstant made by whenMapDispatchToPropsIsMissing:

 wrapMapToPropsConstant((dispatch: Dispatch) => ({
      dispatch,
 }))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it's not just a passthrough

| {
dispatch?: Dispatch
dependsOnOwnProps?: boolean
}
| ActionCreatorsMapObject
| ActionCreator<any>
) {
return function initConstantSelector(dispatch: Dispatch) {
const constant = getConstant(dispatch)
Expand Down
5 changes: 3 additions & 2 deletions src/utils/bindActionCreators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { ActionCreatorsMapObject, Dispatch } from 'redux'
export default function bindActionCreators(
actionCreators: ActionCreatorsMapObject,
dispatch: Dispatch
) {
const boundActionCreators: ActionCreatorsMapObject<any> = {}
): ActionCreatorsMapObject {
const boundActionCreators: ActionCreatorsMapObject = {}

for (const key in actionCreators) {
const actionCreator = actionCreators[key]
if (typeof actionCreator === 'function') {
Expand Down