析构非传播参数后有任何类型

after destructoring non-spread params have any type

我正在尝试创建转换器函数并且它运行良好,但 eslint 向我显示错误

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

问题是房间声明:

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

我需要做什么来修复 eslint 错误消息?

你得到这个错误是因为 IFloorServer 可以是一个对象或 null 因为 | null

  • null
  • 上没有房产
  • ...意思是,不能保证 IFloorServer
  • 上有属性
  • ... 意思是,属性 'rooms' 不能存在于类型 'IFloorServer'