简体   繁体   English

React/Redux 减速器打字稿错误(“未定义”类型不可分配给 ISupplierState 类型)

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

I haven't been able to solve this issue for a long time even though I've been searching through various pages with similar problems.尽管我一直在搜索具有类似问题的各种页面,但我已经很长时间没能解决这个问题了。

I'm receiving the following error:我收到以下错误:

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 {

Here is my reducer:这是我的减速器:

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 };

Here is my types / ISupplierState:这是我的类型/ 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;
}

I understand the error code but I am able to do a very similar approach on another reducer without any issues.我了解错误代码,但我能够在另一个减速器上执行非常相似的方法而没有任何问题。 So I can't see why this is causing so much problem.所以我不明白为什么这会导致这么多问题。

I am using Redux 7.2.0 Any help would be appreciated :) Thank you in advance.我正在使用Redux 7.2.0任何帮助将不胜感激:) 在此先感谢您。

My apologies.我很抱歉。 I had forgot to add a default return to my switch.. So if you encounter a similiar problem, then remember to check your switch!我忘了给我的开关添加一个默认返回..所以如果你遇到类似的问题,那么记得检查你的开关!

Adding following lines solved it.添加以下行解决了它。

default: {
    return state;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React typescript redux 减速器类型“从不” - React typescript redux reducer type 'never' 打字稿错误:类型&#39;undefined []&#39;不能分配给&#39;void&#39;类型 - Typescript error: Type 'undefined[]' is not assignable to type 'void' Redux Reducer中的Action.type未定义错误 - Action.type undefined error in Redux Reducer Typescript - React 错误 TS2322:类型 'AppState | undefined' 不能分配给类型 'ICards | 不明确的' - Typescript - React Error TS2322: Type 'AppState | undefined' is not assignable to type 'ICards | undefined' 打字稿:不可分配给键入错误 - Typescript: is not assignable to type error 输入&#39;字符串| null&#39; 不能分配给类型 &#39;string | TypeScript 的未定义错误 - Type 'string | null' is not assignable to type 'string | undefined error with TypeScript 'string | 类型的参数 undefined' 不可分配给“string”类型的参数 - TypeScript 错误 - Argument of type 'string | undefined' is not assignable to parameter of type 'string' - TypeScript error Typescript 错误:键入此不能分配给键入此 - Typescript error: type this is not assignable to type this Typescript Redux 减速器文件中 action.payload 的类型错误 - Typescript Redux Type error of action.payload in reducer file React 组件上的 Typescript 错误:“元素”类型的参数不可分配给类型的参数 - Typescript error on React Component: Argument of type 'Element' is not assignable to parameter of type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM