Javascript 在TypeScript中添加新属性JSON

Javascript 在TypeScript中添加新属性JSON,javascript,json,angular,typescript,Javascript,Json,Angular,Typescript,我有一个问题,我需要在对象Json中添加新属性,但我在TypeScript中发现了这个问题 错误TS2339:类型{字段: 字符串;值:字符串;运算符:字符串;}' 这是我的代码: var request = [{ field: "company_id", value: this.authService.getCurrentCompany().af_row_id, operator: '=' }]; if (categoryId != null

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

错误TS2339:类型{字段: 字符串;值:字符串;运算符:字符串;}'

这是我的代码:

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

您可以为
请求
定义一个类型,也可以只向对象文本添加一个
条件
字段,并让TypeScript隐式地确定类型。e、 g

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

我选择了第二种解决方案,但遇到了以下问题:错误TS7018:对象文字的属性“条件”隐式具有“any”类型。如果不希望typescript抱怨此类问题,请从编译器标志中删除
“noImplicitAny”:true
选项。否则,您必须明确声明此
条件的类型:未定义
条件:未定义为任何
也是此处的选项,您可以脏掉并执行
请求[0]['condition']=”和“
因为TS是JS的超集。您可以说数组中的项的类型为
any
var-request:any[]=[…]