类型“string”不可分配给类型“never”。ts(2322) const headerKey: never

问题描述 投票:0回答:1

我想问为什么这个特定的行:

rowData[headerKey] = value;

给我这个错误:

类型“string”不可分配给类型“never”.ts(2322) const headerKey:从不

这是代码:

const exportData = (
    table.getSortedRowModel().rows.length > 0
      ? table.getSortedRowModel().rows // Use sorted data if available
      : table.getCoreRowModel().rows
  ).map((row) => {
    const rowData: Partial<TableDataGeneralTemp> = {};
    row.getVisibleCells().forEach((cell) => {
      if (cell.column.columnDef.header !== "IsSelected") {
        const value = cell.getValue() as string;
        const headerKey = cell.column.columnDef.header as keyof TableDataGeneralTemp; 

        rowData[headerKey] = value;
      }
    });
    return rowData;
  });

以下是接口:

interface TableData1 {
  application_date: string; // ISO 8601 date string
  application_id: string;
  applicant_name: string;
  institution?: string;
  applicant_email?: string;
  status: string;
  payment_status: string;
}

interface TableData2 {
  document_Request_Workflow_No: string;
  document_Request_Name: string;
  status: string;
  created: string;
  last_Updated: string;
}

interface TableData3 {
  invoice_no: string;
  gross_revenue: string;
  payment_status: string;
  payment_method: string;
  platform_fee: string;
  net_revenue: string;
}

interface DocStatusData {
  document_request_status_no: string;
  application_id: string;
  document_name: string;
  type_of_copy: string;
  status: string;
}

interface InvoiceData {
  invoice_date: string;
  invoice_no: string;
  payment_date: string;
  amount: string;
  status: string;
}

interface ReceiptData {
  receipt_date: string;
  transaction_no: string;
  payment_date: string;
  status: string;
}

// interface TableData4 {
//   audit_log_no: string;
//   timestamp: string;
//   user: string;
//   activity: string;
//   status: string;
// }

interface CommonTableData {
  // uniqueColumn: string;
  isSelected: boolean;
}

type TableDataGeneralTemp =
  | TableData1
  | TableData2
  | TableData3
  | DocStatusData
  | InvoiceData
  | ReceiptData;
type TableDataGeneral = (
  | TableData1
  | TableData2
  | TableData3
  | DocStatusData
  | InvoiceData
  | ReceiptData
) &
  CommonTableData;

我尝试添加此安全检查:

if (headerKey && headerKey in rowData) {
    rowData[headerKey as keyof TableDataGeneralTemp] = value;
}

然而,并没有成功。该函数过去不会生成任何类型错误,直到有人将其代码添加到组件中,现在我不知道如何消除类型错误。

typescript types casting runtime-error typeerror
1个回答
0
投票

代替:

if (headerKey && headerKey in rowData) {
    rowData[headerKey as keyof TableDataGeneralTemp] = value;
}

尝试:

(rowData as any)[headerKey] = value;
© www.soinside.com 2019 - 2024. All rights reserved.