[英]Typescript error; any Property 'button' does not exist on type 'CellDetails | ContentCellUpdate'
I am wanting to check if a property exists on an object, but the property only exists CellDetails
not on ContentCellUpdate
我想检查 object 上是否存在属性,但该属性仅存在于
CellDetails
上,而不存在于ContentCellUpdate
上
Currently I am doing,目前我正在做,
if(cell.button)
cell can either by CellDetails or ContentCellUpdate单元格可以通过 CellDetails 或 ContentCellUpdate
but I get the error in TS,但我在 TS 中得到错误,
Property 'button' does not exist on type 'CellDetails |
'CellDetails | 类型上不存在属性'按钮' ContentCellUpdate'.
内容单元更新'。 Property 'button' does not exist on type 'ContentCellUpdate'
“ContentCellUpdate”类型上不存在属性“按钮”
The interfaces for each look like this,每个接口看起来像这样,
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;
}
and和
export interface ContentCellUpdate {
row: number;
column: string;
content: ContentCellFormat;
}
I assume that I could add,我想我可以添加,
button: ActionsButton |按钮:动作按钮 | null;
null;
to ContentCellUpdate, but this feels like fixing the symptom rather than the cause?到 ContentCellUpdate,但这感觉像是解决症状而不是原因? Is there a better way to get around this TS error?
有没有更好的方法来解决这个 TS 错误?
You can use hasOwnProperty()
method, your code will be something like this:您可以使用
hasOwnProperty()
方法,您的代码将是这样的:
if (cell.hasOwnProperty('button')) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.