簡體   English   中英

Typescript 錯誤; 'CellDetails | 類型上不存在任何屬性'按鈕' 內容單元更新'

[英]Typescript error; any Property 'button' does not exist on type 'CellDetails | ContentCellUpdate'

我想檢查 object 上是否存在屬性,但該屬性僅存在於CellDetails上,而不存在於ContentCellUpdate

目前我正在做,

if(cell.button)

單元格可以通過 CellDetails 或 ContentCellUpdate

但我在 TS 中得到錯誤,

'CellDetails | 類型上不存在屬性'按鈕' 內容單元更新'。 “ContentCellUpdate”類型上不存在屬性“按鈕”

每個接口看起來像這樣,

export interface CellDetails {
  row: number;
  col: any;
  column: any;
  content: ContentCell;
  header: boolean;
  button: ActionsButton | null;
  reference: string;
  history: boolean;
  link: IPageContentLink | null;
  image: boolean;
  isColumnCheckbox: boolean;
  permission: string;
  inputStyles: string;
  cell: HTMLDivElement;
}

export interface ContentCellUpdate {
  row: number;
  column: string;
  content: ContentCellFormat;
}

我想我可以添加,

按鈕:動作按鈕 | null;

到 ContentCellUpdate,但這感覺像是解決症狀而不是原因? 有沒有更好的方法來解決這個 TS 錯誤?

您可以使用hasOwnProperty()方法,您的代碼將是這樣的:

if (cell.hasOwnProperty('button')) {

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM