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