【问题标题】:Why am i getting this error about overload calls?为什么我会收到有关重载调用的错误消息?
【发布时间】: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


    【解决方案1】:

    因为weatherApp 是函数,而createStore 期望第一个参数是对象。

    如果你有 rootReducer,请将 rootReducer 作为第一个参数传递。但首先,做出改变 const rootReducer = combineReducers({ weatherApp: weatherApp });

    【讨论】:

    • 非常感谢您,非常感谢您,真的
    • 但是现在函数 changeCity 返回 undefined ;-
    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2022-12-20
    相关资源
    最近更新 更多