'Update the Object from object parameter which contain same keys but got TS error

In My Dashboard file I'm passing data like thiss

updateDashboardApiData({
  volumeData: [],
  volumeReportData: [],
})

In my store file

  1. Receive the params
  2. Extract the Object keys
  3. Update the correspondent data
interface ChartDataRow {
  customer: number;
  month: string;
}
type ChartReportData = Record<string | number, ChartDataRow[]>;

type VolumeDataRow = {
  id: number;
  label: string;
  value: string;
};
type VolumeData = VolumeDataRow[];

type VolumeReportDataRow = {
  id: number;
  month: string;
  amount: string;
  point: number;
  volume: Array<string | number>;
};
type VolumeReportData = VolumeReportDataRow[];
interface DashboardAPIData {
  chartReportData: ChartReportData;
  volumeData: VolumeData;
  volumeReportData: VolumeReportData;
}

const dashboard: DashboardAPIData = {
  chartReportData: {},
  volumeData: [],
  volumeReportData: [],
};

type DashboardAPIDataKey = Extract<keyof DashboardAPIData, string>;

export const updateDashboardApiData = (payload: Partial<DashboardAPIData>) => {
  if (payload && payload?.constructor === Object) {
    Object.keys(payload).forEach((key) => {
      const keyVal = key as DashboardAPIDataKey;
      if (dashboard[keyVal] !== undefined && payload[keyVal] !== undefined) {
        dashboard[keyVal] = payload[keyVal]; \\dashboard[keyVal] show error by ts
      }
    });
  }
};

All functionality this are fine. but TS shows the error like

Type 'ChartReportData | VolumeData | VolumeReportData | undefined' is not assignable to type 'WritableDraft<ChartReportData> & WritableDraft<VolumeDataRow>[] & WritableDraft<VolumeReportDataRow>[]'.
  Type 'undefined' is not assignable to type 'WritableDraft<ChartReportData> & WritableDraft<VolumeDataRow>[] & WritableDraft<VolumeReportDataRow>[]'.
    Type 'undefined' is not assignable to type 'WritableDraft<ChartReportData>'.ts(2322)

Why i getting Error like this. i tried but i can't found where i mistaken



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source