类型 'any[]' 不能用作索引类型 Angular 构建

加拿大11

当我使用ng serve一切正常。当我使用ng serve --prod --aot它时抱怨Type 'any[]' cannot be used as an index type. 我找不到任何可以修复它的东西。并且它似乎所指的“type any[]”是 tags 变量,因为当我将其注释掉时,一切都会编译。

这是相关的代码:

//cluster-details.component.ts
...
export class ClusterDetailsComponent implements OnInit {
infoTitles = {
    edgeNodePublicDNS: 'Edge Node Public DNS',
    ...
    tags: 'Tags'
};
infoKeys: string[];
info: any;
tags: any[];
...
this.requestsService.emrinstanceDetailsGET(body)
        .subscribe(
            (data) => {
                this.infoKeys = Object.keys(data);
                this.info = data;
...

集群详细信息.component.html:

<tr *ngFor="let key of infoKeys">
            <td>
                <strong>{{ infoTitles[key] }}:</strong>
            </td>
            <td>
                {{info[key]}}
            </td>
        </tr>
        <tr>
            <td>
                <strong>{{ infoTitles[tags] }}:</strong>
            </td>
            <td>
                {{tags}}
            </td>
        </tr>

请求返回如下数据:

.map(
      (response: Response) => {
          const data = response.json();
          return data['clusterDetail'];

现在我知道'只是不要让它输入'any[]',但我不知道要怎么做。返回的是一个 JSON 值,它只是一个对象类型。将类型更改为 Object 不起作用,我看不到任何其他类型的工作。也许我必须为它制作一个界面?

加拿大11

弄清楚了。一个我不打算使用的变量的简单误用:

HTML 行...{{ infoTitles[tags] }}...试图用标签变量索引 infoTitles - 我的意思是这是一个只是“标签”的字符串,其中它将正确引用我需要的 infoTitles 值。但我没有发现我正在使用标签数组。所以修复应该只是使用一个字符串或变量,就像tagIndexString = 'tags'HTML 可以是...{{ infoTitles[tagIndexString] }}...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类型 '() => Promise<AxiosResponse<any>>' 不能用作索引类型 [ReactJs]

类型'null'不能用作索引类型

类型“{}”不能用作索引类型

类型“X”不能用作索引类型

类型不能用作索引类型

类型'undefined'不能用作索引类型

打字稿错误-类型'string []'不能用作索引类型

类型“未定义”不能用作索引类型

Typescript:类型'undefined'不能用作索引类型.ts(2538)

元素隐式地具有“ any”类型,因为类型“ any”的表达式不能用于索引类型

元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引“Breakpoints”类型

元素隐式地具有“ any”类型,因为类型为string | 数字不能用于索引类型

元素隐式地具有“ any”类型,因为类型“ string”的表达式不能用于索引类型

元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引“Palette”类型

元素隐式具有 'any' 类型,因为类型 'string' 的表达式不能用于索引类型 '{ AT: number; 是:数字,...}`

Typescript错误:元素隐式具有“ any”类型,因为类型“ string”的表达式不能用于索引类型

TypeScript:隐式具有“ any”类型,因为类型“ string”的表达式不能用于索引类型

TypeScript - 元素隐式具有“any”类型,因为“storyFile”类型的表达式不能用于索引类型“{}”

元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引“PointDto”类型

该元素隐式具有类型“any”,因为类型“string”的表达式不能用于索引该类型

Haskell:如何构建异构类型Any

元素隐式具有 any 类型,因为时间表达式 any 不能用于索引类型

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

为什么我在TypeScript中遇到“类型'String'不能用作索引类型”错误

响应 yup 错误类型“未定义”不能用作索引类型。TS2538

元素隐式具有“any”类型,因为“Tag”类型的表达式不能用于索引类型“{}”。类型“{}”上不存在属性“html”

介质不能用作请求的设备类型

TypeScript-元素隐式地具有“ any”类型,因为类型“ string”的表达式不能用于索引类型

元素隐式具有 'any' 类型,因为类型 'string' 的表达式不能用于索引类型 {"1" : { "key": string}; "2" : { "key": 字符串};}