javascript - 在 TypeScript 中添加新属性 JSON

标签 javascript json angular typescript

我有一个问题,我需要在对象 Json 中添加新的属性,但我在 TypeScript 中发现了这个问题

error TS2339: Property 'condition' does not exist on type '{ field: string; value: string; operator: string; }'

这是我的代码:

var request = [{
      field: "company_id",
      value: this.authService.getCurrentCompany().af_row_id,
      operator: '='
    }];
    if (categoryId != null) {
      request[0].condition = "AND";
    }

最佳答案

您可以为 request 定义一个类型,或者您可以只向对象字面量添加一个 condition 字段并让 TypeScript 隐式确定类型。例如

var request = [{
    field: "company_id",
    value: this.authService.getCurrentCompany().af_row_id,
    operator: '=',
    condition: undefined
}];
if (categoryId != null) {
    request[0].condition = "AND";
}

关于javascript - 在 TypeScript 中添加新属性 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41049481/

相关文章:

angular - 将表单字段动态添加到 Angular 8 中的 react 表单

javascript - Cypress 测试的随机元素选择

javascript - 修改函数输出 - 检查源 - Javascript - HTML - Google Chrome

javascript - 具有 Angular 5 的 Pinterest 风格砌体网格

angular - 如何检测 Angular Universal 预渲染版本?

java - Realm createOrUpdateAllFromJson 创建 RealmModel 对象而不持久化?

javascript - ASP.NET MVC 重新加载页面表单重新提交并带有警报

javascript - 单击时重复输入,在单击的元素之前插入 - jQuery

php - 如何在android studio上使用webview获取json请求?

json - 使用 Zend Framework 2 将 Doctrine 2 实体持久集合转换为数组的最佳方法