Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

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

$
0
0

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


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>