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

Type issue with ActionCreators from createSlice with no payload #108

Closed
pat-son opened this issue Feb 4, 2019 · 3 comments
Closed

Type issue with ActionCreators from createSlice with no payload #108

pat-son opened this issue Feb 4, 2019 · 3 comments

Comments

@pat-son
Copy link

pat-son commented Feb 4, 2019

I am creating a slice for my state, and one of the actions I want to create does not take a payload. However, when mapping the dispatch to the props in connect, I get a weird type error.
image
Argument of type 'typeof MyComponent' is not assignable to parameter of type 'ComponentType<Matching<{ loading: boolean; } & { setLoaded: SliceActionCreator<void>; }, MyProps>>'. Type 'typeof MyComponent' is not assignable to type 'ComponentClass<Matching<{ loading: boolean; } & { setLoaded: SliceActionCreator<void>; }, MyProps>, any>'. Types of parameters 'props' and 'props' are incompatible. Type 'Matching<{ loading: boolean; } & { setLoaded: SliceActionCreator<void>; }, MyProps>' is not assignable to type 'Readonly<MyProps>'. Types of property 'setLoaded' are incompatible. Type 'SliceActionCreator<void>' is not assignable to type '() => void'.

Here's a minimal reproduction of the issue I'm having:

import React, { PureComponent } from 'react';
import { createSlice } from 'redux-starter-kit';
import { connect } from 'react-redux'

interface MyState {
    loading: boolean;
}

const INITIAL_STATE: MyState = {
    loading: true,
}

const slice = createSlice({
    slice: 'mySlice',
    initialState: INITIAL_STATE,
    reducers: {
        setLoaded: (state) => { state.loading = false; },
    }
})

interface MyProps {
    loading: boolean;
    setLoaded(): void;
}

class MyComponent extends PureComponent<MyProps> {
    componentDidMount() {
        // perform some check for loading
        const setLoaded = this.props.setLoaded;

        setLoaded();
    }

    render() {
        const loading = this.props.loading;
        if (loading) {
            return (
                <p>Loading...</p>
            );
        } else {
            return (
                <p>Loaded!</p>
            )
        }
    }
}

export default connect(
    (state: MyState) => ({
        loading: state.loading,
    }),
    {
        setLoaded: slice.actions.setLoaded,
    }
)(MyComponent)

If I change the type of the action property in MyProps to take a void payload, then the error goes away, but that's obviously not the correct type:

interface MyProps {
    loading: boolean;
    setLoaded(payload: void): void;
}

Am I doing something wrong, or is there a bug in the typing?

@pat-son pat-son changed the title Issue with ActionCreators from createSlice with no payload Type issue with ActionCreators from createSlice with no payload Feb 5, 2019
@denisw
Copy link
Contributor

denisw commented Feb 5, 2019

This is indeed a typing bug. I created a PR (#110) which fixes the issue.

@pat-son
Copy link
Author

pat-son commented Feb 5, 2019

Awesome, thanks

@pat-son
Copy link
Author

pat-son commented May 16, 2019

Fixed by #133.

@pat-son pat-son closed this as completed May 16, 2019
markerikson pushed a commit that referenced this issue Apr 20, 2021
* Allow more flexible headers that can be undefined
* Adds doc blocks for JS users to `fetchBaseQuery`

Co-authored-by: Lenz Weber <mail@lenzw.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants