简体   繁体   English

如果我尝试使用 Angular 中的方括号动态访问属性值,Typescript 将显示错误

[英]Typescript is showing error if I try to access the property value dynamically using square brackets in Angular

How to Fix No index signature with a parameter of type 'string' issue in Angular如何修复Angular 中带有“字符串”类型参数的无索引签名问题

Getting ESLint error here: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Client'.此处出现 ESLint 错误:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“客户端”。 No index signature with a parameter of type 'string' was found on type 'Row'在“行”类型上找不到具有“字符串”类型参数的索引签名

dynamic-table.component.html动态表.component.html

<table>
  <tr>
    <th *ngFor="let i of headers">
      {{i.name}}
    </th>
  </tr>
  <tr *ngFor="let row of rows">
    <td *ngFor="let head of headers">
      {{row[head.name]}} <!-- Getting ESLint error here : Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Client'.
  No index signature with a parameter of type 'string' was found on type 'Row'. -->
    </td>
  </tr>
</table>

dynamic-table.component.ts动态表.component.ts

interface Header {
 displayName: string,
 name: string
}

interface Row {
 empId: string,
 empName: string,
 salary: string
}

@Component({
  selector: "dynamic-table",
  templateUrl: "./dynamic-table.component.html",
  styleUrls: []
})
export class DynamicTableComponent {
  title = "Dynamic Table";

  headers: Header[] = [];
  rows: Row[] = [];

  constructor() {
    this.headers = [
      {
        displayName: "Emp Name",
        name: "empName"
      },
      {
        displayName: "Emp ID",
        name: "empId"
      },
      {
        displayName: "Salary",
        name: "salary"
      }
    ];
    this.rows = [
      {
        empId: "1",
        empName: "red",
        salary: "10000"
      },
      {
        empId: "1",
        empName: "red",
        salary: "50000"
      },
      {
        empId: "1",
        empName: "red",
        salary: "30000"
      }
    ];
  }
}

In version 13 Angular CLI enables TypeScript strict mode on newly created projects.在版本 13 中,Angular CLI 对新创建的项目启用 TypeScript strict模式。 That enables (among other) the noImplicitAny flag.这启用(除其他外) noImplicitAny标志。

Since header.name could be any string TypeScript has no way to know the type of row[header.name] so it's assigning it to any (and is complaining about that).由于header.name可以是任何字符串 TypeScript 无法知道row[header.name]的类型,因此它将其分配给any字符串(并且正在抱怨)。

To solve the issue you can do any of the followings:要解决此问题,您可以执行以下任何操作:

(1) declare an index signature on your Row interface: (1) 在你的Row接口上声明一个索引签名:

interface Row {
  [key: string]: string,
  empId: string,
  empName: string,
  salary: string
}

Here you're telling TS that any property on Row accessed through a string will have a string type.在这里,您告诉 TS,通过字符串访问的Row上的任何属性都将具有string类型。

(2) restrict the type of Header.name to a literal union of Row 's keys: (2) 将Header.name的类型限制为Row键的文字联合:

interface Header {
    displayName: string,
    name: keyof Row
}

Here you're declaring the type of Header.name as "empId" | "empName" | "salary"在这里,您将Header.name的类型声明为"empId" | "empName" | "salary" "empId" | "empName" | "salary" "empId" | "empName" | "salary" (no other value will be allowed). "empId" | "empName" | "salary" (不允许其他值)。

(3) Disable the noImplicitAny flag by setting "noImplicitAny": false in your tsconfig.json (obviously this will disable the check at all). (3) 通过在tsconfig.json中设置"noImplicitAny": false来禁用noImplicitAny标志(显然这将完全禁用检查)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Angular 7中使用打字稿获取属性值 - How to get value of property using typescript in angular 7 在 ANGULAR 中使用 Typescript 的错误属性 _id - Error property _id using Typescript in ANGULAR 错误类型错误:_co.userData 未定义在 angular 7 中,当我尝试使用属性绑定作为值时 - ERROR TypeError: _co.userData is undefined In angular 7 when I try to use property binding for value 如何访问或读取 angular typescript 文件中的 css 属性值 - How to access or read the css property value in angular typescript file 错误当我尝试使用拖放操作添加带有angular2的动态组件时 - Error When I try to add dynamically component with angular2 using drag and drop 绑定 Angular 从方括号分配(值包括 =“[...]”) - binding Angular assigning from Square Brackets (value including ="[...]") 我正在使用angular 2 angular-cli并尝试使用ng2-datetime,但出现错误“ TypeError:无法读取未定义的属性&#39;jQuery&#39;” - I am using angular 2 angular-cli and try to use ng2-datetime,but it give error “TypeError: Cannot read property 'jQuery' of undefined” 使用角度/打字稿中的另一个函数为接口属性赋值? - Assign a value to an interface property using a function on another in angular / typescript? 我可以在 Angular 模板中使用带方括号属性访问的安全运算符吗? - Can I use the safe operator with square bracket property access in Angular template? Angular2 动态访问这个对象打字稿 - Angular2 dynamically access this object typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM