【发布时间】:2020-07-12 04:55:26
【问题描述】:
下午好,我在使用 createStore 时收到此错误,谁能解释一下我做错了什么?
TypeScript error in /workspace/weatherAppTypescript/App/appfrontend/src/redux/store/index.ts(7,31):
No overload matches this call.
Overload 1 of 2, '(reducer: Reducer<CityProps, any>, enhancer?: StoreEnhancer<unknown, unknown> | undefined): Store<CityProps, any>', gave the following error.
Argument of type '(state: CityProps | undefined, action: any) => { temp: any; feelslike: any; cityname: any; weatherdesc: any; } | { cityname: any; temp?: undefined; feelslike?: undefined; weatherdesc?: undefined; }' is not assignable to parameter of type 'Reducer<CityProps, any>'.
Type '{ temp: any; feelslike: any; cityname: any; weatherdesc: any; } | { cityname: any; temp?: undefined; feelslike?: undefined; weatherdesc?: undefined; }' is not assignable to type 'CityProps'.
Type '{ cityname: any; temp?: undefined; feelslike?: undefined; weatherdesc?: undefined; }' is not assignable to type 'CityProps'.
Types of property 'temp' are incompatible.
Type 'undefined' is not assignable to type 'number'.
Overload 2 of 2, '(reducer: Reducer<CityProps, any>, preloadedState?: { temp: number; feelslike: number; cityname: string; weatherdesc: string; } | undefined, enhancer?: StoreEnhancer<unknown, {}> | undefined): Store<...>', gave the following error.
Argument of type '(state: CityProps | undefined, action: any) => { temp: any; feelslike: any; cityname: any; weatherdesc: any; } | { cityname: any; temp?: undefined; feelslike?: undefined; weatherdesc?: undefined; }' is not assignable to parameter of type 'Reducer<CityProps, any>'. TS2769
5 |
6 | export default function configureStore() {
> 7 | const store = createStore(weatherApp);
| ^
8 | return store
9 | }
这是我的商店:
import { createStore, combineReducers } from 'redux';
const rootReducer = combineReducers({weatherApp,});
export default function configureStore() {
const store = createStore(weatherApp);
return store
}
这是我的减速器:
temp: 0,
feelslike: 0,
cityname: "London",
weatherdesc: ""
}
export function weatherApp(state = initialState, action: any) {
switch (action.type) {
case 'changeCity': {
return {
temp: action.temp,
feelslike: action.feelslike,
cityname: action.cityname,
weatherdesc: action.weatherdesc
}
}
case 'changeAPI': {
return {
cityname: action.cityname
}
}
default:
return state;
}
这是 CityProps 界面:
export interface CityProps {
temp: number;
feelslike: number;
cityname: string;
weatherdesc: string;
}
非常感谢!我真的不知道如何解决这个问题,我已经尝试了几个小时......非常感谢任何帮助我的人^^
【问题讨论】:
标签: reactjs typescript redux