[英]error TS2538: Type 'CCCustomer' cannot be used as an index type
For below mentioned code facing error as error TS2538: Type 'CCCustomer' cannot be used as an index type.对于下面提到的代码面临错误为错误 TS2538:类型“CCCustomer”不能用作索引类型。 with let index in ccCustomerList its working fine but in sonarqube received issue as Use "for...of" to iterate over this "Array".
使用 ccCustomerList 中的 let 索引它的工作正常,但在 sonarqube 中收到的问题是使用“for...of”来迭代这个“数组”。 any alternative way available for this.
任何可用的替代方式。
let data:Array<Customer>;
for (const index of CustomerList) {
const Customer = CustomerList[index];
const List: Array<string> = [];
In your loop, the index
variable is not really an index of the array, but it's an object from the Customerlist
, this is how for of
works and that's why you get the error stating you can't use an object as an index.在您的循环中,
index
变量实际上并不是数组的索引,而是来自Customerlist
的一个对象,这就是for of
工作方式,这就是为什么您会收到错误消息,指出您不能将对象用作索引。
So you don't need to get the customer from the CustomerList
array since it's already there in your index variable.因此,您不需要从
CustomerList
数组中获取客户,因为它已经存在于您的索引变量中。
Rename index
to Customer
and remove this line:将
index
重命名为Customer
并删除此行:
const Customer = CustomerList[index];
Replace all CustomerList[index]
with Customer
in your code.将代码中的所有
CustomerList[index]
替换为Customer
。
for (const Customer of CustomerList) {
const guidList: Array<string> = [];
// Use Customer object
If you need to use an index use a regular loop or add Array.entries()
to the current loop which returns the index and the value (just remove it if not needed).如果您需要使用索引,请使用常规循环或将
Array.entries()
添加到返回索引和值的当前循环(如果不需要,只需将其删除)。
for (const [index, value] of CustomerList.entries()) {
const Customer = CustomerList[index];
const guidList: Array<string> = [];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.