【发布时间】:2020-02-16 10:48:01
【问题描述】:
我无法输入 mapStateToProps 的“状态”参数
如果我只是更改 state : any 而不是 state: AppState 它可以工作并且没有错误。
但我想为我的状态参数输入正确的类型。
现在,我在 connect() 的 mapStateToProps 参数上有这个错误
没有重载匹配这个调用。 最后一个重载给出了以下错误。 '(state: { quiz: IQuizInitialState; }) => StateProps' 类型的参数不可分配给'MapStateToPropsParam' 类型的参数。 无法将类型 '(state: { quiz: IQuizInitialState; }) => StateProps' 分配给类型 'MapStateToPropsFactory'。 参数“状态”和“初始状态”不兼容。 类型“{}”中缺少属性“测验”,但类型“{测验: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)
这是我的 rootReducer :
import { combineReducers } from 'redux';
import { QuizReducer } from './quiz';
const rootReducer = combineReducers({
quiz: QuizReducer
});
export type AppState = ReturnType<typeof rootReducer>
export default rootReducer;
单个reducer是:
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
}
}
提前谢谢你们!
【问题讨论】:
-
mapStateToProps 中的 AppState 是什么?
标签: reactjs typescript redux typing