简体   繁体   English

错误 TS2538:类型“CCCustomer”不能用作索引类型

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

Example例子

for (const Customer of CustomerList) {
  const guidList: Array<string> = [];
  // Use Customer object

More information 更多信息

Update更新

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()添加到返回索引和值的当前循环(如果不需要,只需将其删除)。

Example例子

for (const [index, value] of CustomerList.entries()) {
  const Customer = CustomerList[index];
  const guidList: Array<string> = [];

暂无
暂无

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

相关问题 src/index.ts:12:22 - 错误 TS2307:找不到模块“./schema.graphql”或其相应的类型声明 - src/index.ts:12:22 - error TS2307: Cannot find module './schema.graphql' or its corresponding type declarations 类型“undefined”不能用作索引类型。 如何解决? - Type 'undefined' cannot be used as an index type. How to fix it? 在“Query”.ts 类型上未找到带有“string”类型参数的索引签名 - No index signature with a parameter of type 'string' was found on type 'Query'.ts 错误TS2693:“地图”仅引用类型,但在此处被用作值 - error TS2693: 'Map' only refers to a type, but is being used as a value here TypeScript NodeJS 应用程序错误:元素隐式具有“任何”类型,因为类型“typeof globalThis”没有索引签名.ts(7017) - TypeScript NodeJS application Error: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.ts(7017) 打字稿错误TS2688:找不到“ .AppleDouble”的类型定义文件 - Typescript error TS2688: Cannot find type definition file for '.AppleDouble' Elastic Beanstalk“错误 TS2688:找不到‘节点’的类型定义文件。” - Elastic Beanstalk "error TS2688: Cannot find type definition file for 'node'." TS2307 错误:找不到模块“../constants”或其相应的类型声明 - TS2307 Error: Cannot find module '../constants' or its corresponding type declarations tsc 报告错误 TS2318:找不到全局类型 &#39;Boolean&#39; - tsc report error TS2318: Cannot find global type 'Boolean' 错误 TS2307:找不到模块或其对应的类型声明。 在 Github 操作 - error TS2307: Cannot find module or its corresponding type declarations. on Github Actions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM