Search code examples
typescripteslinttypescript-eslint

after destructoring non-spread params have any type


I'am trying to create converter function and it works good, but eslint is showing me error

export const convertRoomsInFloorToObj = (floors: IFloorServer[]): IFloor[] =>
  floors.map(({rooms, ...another}: IFloorServer) => ({
    rooms: rooms // Property 'rooms' does not exist on type 'IFloorServer'
.reduce(
     // operation with array
    ),
    ...another,
  }));

The problem is that rooms is declared:

export type IFloor = {
  floor_number: number;
  count_of_rooms: number;
  rooms: IRooms;
};

export type IFloorServer = {
  floor_number: number;
  count_of_rooms: number;
  rooms: IRooms; // here
} | null;

export type IRoom = {
  // Room props
};

What I have to do to fix eslint error message?


Solution

  • You get this error because IFloorServer can be an object or null because of | null

    • there are no properties on null
    • ... meaning, there's no guarantee that there are properties on IFloorServer
    • ... meaning, property 'rooms' cannot exist on type 'IFloorServer'