[英]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.