Skip to content

Commit

Permalink
fix: type when passing nullish mapDispathToProps
Browse files Browse the repository at this point in the history
  • Loading branch information
marconi1992 committed Jun 23, 2022
1 parent 2dc6c4e commit 9b33710
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/components/connect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ export interface Connect<DefaultState = unknown> {
TOwnProps
>

/** mapState and mapDispatch (nullish) */
<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = DefaultState>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
mapDispatchToProps: null | undefined
): InferableComponentEnhancerWithProps<TStateProps, TOwnProps>

/** mapState and mapDispatch (as an object) */
<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = DefaultState>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
Expand Down
61 changes: 61 additions & 0 deletions test/typetests/connect-mapstate-mapdispatch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,40 @@ function MapStateAndDispatchObject() {
const verify = <Test foo="bar" />
}

function MapStateAndNullishDispatch() {
interface ClickPayload {
count: number
}
const onClick: ActionCreator<ClickPayload> = () => ({ count: 1 })
const dispatchToProps = {
onClick,
}

interface OwnProps {
foo: string
}
interface StateProps {
bar: number
}

const mapStateToProps = (_: any, __: OwnProps): StateProps => ({
bar: 1,
})

class TestComponent extends React.Component<OwnProps & StateProps> {}

const TestDispatchPropsNull = connect(mapStateToProps, null)(TestComponent)

const verifyNull = <TestDispatchPropsNull foo="bar" />

const TestDispatchPropsUndefined = connect(
mapStateToProps,
undefined
)(TestComponent)

const verifyNonUn = <TestDispatchPropsUndefined foo="bar" />
}

function MapDispatchFactory() {
interface OwnProps {
foo: string
Expand Down Expand Up @@ -422,6 +456,33 @@ function MapStateAndDispatchAndMerge() {
const verify = <Test foo="bar" />
}

function MapStateAndMerge() {
interface OwnProps {
foo: string
}
interface StateProps {
bar: number
}
interface DispatchProps {
onClick: () => void
}

class TestComponent extends React.Component<OwnProps & StateProps> {}

const mapStateToProps = () => ({
bar: 1,
})

const mergeProps = (stateProps: StateProps, _: null, ownProps: OwnProps) => ({
...stateProps,
...ownProps,
})

const Test = connect(mapStateToProps, null, mergeProps)(TestComponent)

const verify = <Test foo="bar" />
}

function MapStateAndOptions() {
interface State {
state: string
Expand Down

0 comments on commit 9b33710

Please sign in to comment.