Search code examples
angulartypescriptngrx

does not exist on type 'never'


Today, I try to write a reducer.ts and got the error inside I define initialstate as below

import { ActionsUnion, ActionTypes } from './actions';
  
export const initialState = {
  items: []  ,
  cart: []
};

while I got the error on below

case ActionTypes.Remove:
      return {
        ...state,
        cart: [...state.cart.filter(item => item.name  !== action.payload.name)]
      };

It state item.name Property 'name' does not exist on type 'never'.ts(2339) in item.name, I know I should create interface for initalstate But I don't know how to do it. Can anyone advise ?

Thank you


Solution

  • You have item typed as [], and property 'name' does not exist on this object. If you don’t care about typing, you can declare item as Array<any>:

    export const initialState = {
      items: [] as Array<any>,
      cart: []
    };
    

    If you want static typing, then you can create an interface for the structure and use it:

    export interface item {
    name: string;
    }
    
    let items: Array<item>;