Skip to content
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

Fix wrapped component prop types when passing nullish mapDispatchToProps #1928

Merged
merged 1 commit into from Nov 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components/connect.tsx
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
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