简体   繁体   English

Typescript 错误; 'CellDetails | 类型上不存在任何属性'按钮' 内容单元更新'

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

相关问题 打字稿类型错误属性不存在 - Typescript type error property does not exist Angular4打字稿; 属性“ data”在类型“ any []”上不存在 - Angular4 Typescript; Property 'data' does not exist on type 'any[]' 类型{}不存在Typescript属性 - Typescript property does not exist on type {} 类型 [] 打字稿上不存在属性 - property does not exist on type [] typescript 属性在类型上不存在-TypeScript - Property does not exist on type - TypeScript TypeScript构建错误:“ IStateParamsService”类型不存在属性 - TypeScript Build Error : Property does not exist on type 'IStateParamsService' 打字稿错误:类型“元素”上不存在属性“包含”。 - Typescript error :Property 'contains' does not exist on type 'Element'.? "<i>TypeScript error: Property &#39;children&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript 错误:类型 &#39;{ children?: ReactNode; 上不存在属性 &#39;children&#39;<\/b> <i>}&#39;<\/i> }&#39;<\/b>" - TypeScript error: Property 'children' does not exist on type '{ children?: ReactNode; }' 如何修复 Typescript 中的“元素”类型错误不存在属性? - How to fix property does not exist on type 'Element' error in Typescript? 打字稿错误:类型{…}上不存在属性&#39;log&#39;-Console.log() - typescript error: Property 'log' does not exist on type {…} - Console.log()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM