I am not able to type the "state" parameter of a mapStateToProps
If I just change state : any
instead of state: AppState
it works and no error.
But I would like to have a correct typing for my state parameter.
For now, I have this error on the mapStateToProps param of the connect()
No overload matches this call. The last overload gave the following error. Argument of type '(state: { quiz: IQuizInitialState; }) => StateProps' is no assignable to parameter of type 'MapStateToPropsParam'. Cannot assign the type '(state: { quiz: IQuizInitialState; }) => StateProps' to type 'MapStateToPropsFactory'. Parameters 'state' and 'initialState' are not compatible. Property 'quiz' is missing in type '{}' but required in type '{ quiz: IQuizInitialState; }'.ts(2769)
interface OwnProps {
}
interface StateProps {
}
interface DispatchProps {
}
type Props = OwnProps & StateProps & DispatchProps;
export class App extends Component<Props> {
render() {
return (
<div>Hey</div>
);
}
}
const mapStateToProps = (state: AppState): StateProps => ({
})
const mapDispatchToProps = (dispatch: ThunkDispatch<{}, {}, AnyAction>): DispatchProps => {
return {
}
}
// The args 'mapStateToProps' generate the error
export default connect<StateProps,DispatchProps,OwnProps>(mapStateToProps, mapDispatchToProps)(App)
This is my rootReducer :
import { combineReducers } from 'redux';
import { QuizReducer } from './quiz';
const rootReducer = combineReducers({
quiz: QuizReducer
});
export type AppState = ReturnType<typeof rootReducer>
export default rootReducer;
And the single reducer is :
import { TYPES } from '../actions/action-types';
import { IQuizListItem, Action } from '../models/index';
import { AnyAction } from 'redux';
export interface IQuizInitialState {
quizListItem: IQuizListItem[]
}
const quizInitialState: IQuizInitialState = {
quizListItem: []
}
export const QuizReducer = (state = quizInitialState, action: AnyAction): IQuizInitialState => {
switch (action.type) {
case TYPES.getQuizListItems:
return {
...state,
quizListItem: (action as Action<IQuizListItem[]>).payload
}
default:
return state
}
}
Thank you by advance guys !