簡體   English   中英

React/Redux 減速器打字稿錯誤(“未定義”類型不可分配給 ISupplierState 類型)

[英]React/Redux reducer typescript error (Type 'undefined' is not assignable to type ISupplierState)

盡管我一直在搜索具有類似問題的各種頁面,但我已經很長時間沒能解決這個問題了。

我收到以下錯誤:

src/app/store/instructor/supplier/reducer.tsx
TypeScript error in src/app/store/instructor/supplier/reducer.tsx(11,7):
Type '(state: ISupplierState | undefined, action: AnyAction) => { loading: true; suppliers: Supplier[]; errors?: string | undefined; } | { suppliers: any; loading: false; errors?: string | undefined; } | { ...; } | undefined' is not assignable to type 'Reducer<ISupplierState, AnyAction>'.
  Type '{ loading: true; suppliers: Supplier[]; errors?: string | undefined; } | { suppliers: any; loading: false; errors?: string | undefined; } | { loading: false; errors: any; suppliers: Supplier[]; } | undefined' is not assignable to type 'ISupplierState'.
    Type 'undefined' is not assignable to type 'ISupplierState'.  TS2322

     9 | };
    10 | 
  > 11 | const reducer: Reducer<ISupplierState> = (state = initialState, action) => {
       |       ^
    12 |   switch (action.type) {
    13 |     case ISupplierActionTypes.ISUPPLIER_FETCH: {
    14 |       return {

這是我的減速器:

export const initialState: ISupplierState = {
  suppliers: [],
  loading: false,
  errors: undefined,
};

const reducer: Reducer<ISupplierState> = (state = initialState, action) => {
  switch (action.type) {
    case ISupplierActionTypes.ISUPPLIER_FETCH: {
      return {
        ...state,
        loading: true,
      };
    }
    case ISupplierActionTypes.ISUPPLIER_SUCCESS: {
      return {
        ...state,
        suppliers: action.payload,
        loading: false,
      };
    }
    case ISupplierActionTypes.ISUPPLIER_ERROR: {
      return {
        ...state,
        loading: false,
        errors: action.payload,
      };
    }
  }
};

export { reducer as iSupplierReducer };

這是我的類型/ ISupplierState:

export interface Supplier {
  name: string;
  id: number;
  payment_terms: number;
  address_id: number;
  delivery_time: number;
  bank_id: number;
  company_Type_id: number;
}

export interface ISupplier {
  suppliers: Supplier[];
}

export enum ISupplierActionTypes {
  ISUPPLIER_FETCH = '@@instructor/supplier/REQUEST',
  ISUPPLIER_SUCCESS = '@@instructor/supplier/SUCCESS',
  ISUPPLIER_ERROR = '@@instructor/supplier/ERROR',
}

export interface ISupplierState {
  readonly loading: boolean;
  readonly suppliers: Supplier[];
  readonly errors?: string;
}

我了解錯誤代碼,但我能夠在另一個減速器上執行非常相似的方法而沒有任何問題。 所以我不明白為什么這會導致這么多問題。

我正在使用Redux 7.2.0任何幫助將不勝感激:) 在此先感謝您。

我很抱歉。 我忘了給我的開關添加一個默認返回..所以如果你遇到類似的問題,那么記得檢查你的開關!

添加以下行解決了它。

default: {
    return state;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM