Skip to content

Commit

Permalink
Merge pull request #1928 from marconi1992/fix/1927
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Nov 4, 2022
2 parents 5082af7 + ff7d96b commit 32e40e4
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 @@ -305,6 +305,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 32e40e4

Please sign in to comment.