typescript 构建:元素隐式具有“任何”类型,因为索引表达式不是“数字”类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36398492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:02:35  来源:igfitidea点击:

Build: element implicitly has an 'any' type because index expression is not of type 'number'

typescripttypescript1.8

提问by Abdelkrim

We are transpiling our TypeScript 1.8 code forcing the software developer to set the types.

我们正在转换我们的 TypeScript 1.8 代码,迫使软件开发人员设置类型。

Here are the error messages:

以下是错误消息:

  • Description: Build: Element implicitly has an 'any' type because index expression is not of type 'number'.
  • Project myProject
  • File: C:\dev\Scripts\machines.ts
  • Lines: 77, 78, 80, 81, 96, 110, 111, 220
  • Source: Build
  • 说明:构建:元素隐式具有“任何”类型,因为索引表达式不是“数字”类型。
  • 项目我的项目
  • 文件:C:\dev\Scripts\machines.ts
  • 线路:77、78、80、81、96、110、111、220
  • 来源:构建

QUESTION

How should we define the types of those variables?

我们应该如何定义这些变量的类型?

  • _matrixQuantityProduct
  • _matrixQuantityLotSizeProduct
  • matrixQuantityProduct2
  • matrixQuantityLotSizeProduct2
  • _matrixQuantityProduct
  • _matrixQuantityLotSizeProduct
  • 矩阵数量产品2
  • matrixQuantityLotSizeProduct2

Source code

源代码

    private _machines: Machine[];
    private _matrixQuantityProduct: Array<Array<number>>;
    private _matrixQuantityLotSizeProduct: Array<Array<number>>;
    private buildMatrixQuantity() {
        let matrixQuantityProduct : Array<Array<number>> = new Array();
        let matrixQuantityLotSizeProduct: Array<Array<number>> = new Array();

        // on cree le array 2D et les clés dynamiques
        for (let i = 0; i < this._machines.length; i++) {
            let matrixQuantityProduct2 : Array<any> = [];
            let matrixQuantityLotSizeProduct2: Array<any>  = [];
            for (let j = 0; j < this._machines.length; j++) {
                    matrixQuantityProduct2[this._machines[j].code] = 0; [line 77]
                matrixQuantityLotSizeProduct2[this._machines[j].code] = 0; [line 78]
            }
            matrixQuantityProduct[this._machines[i].code] = matrixQuantityProduct2; [line 80]
            matrixQuantityLotSizeProduct[this._machines[i].code] = matrixQuantityLotSizeProduct2; [line 80]
        }

        this.listMatrixLog(matrixQuantityProduct, matrixQuantityLotSizeProduct);

        let _productCodeElements: Element[] = this._productModel.getProducts();
        for (let i = 0; i < _productCodeElements.length; i++) {
            let _quantityProduct: number = this._productModel.getQuantityProduct(_productCodeElements[i].nodeValue);
            let _processCodeElements: Element[] = this._productModel.getProcessProduct(_productCodeElements[i].nodeValue);
            for (let j = 0; j < _processCodeElements.length; j++) {

                let _lotSizeOperation: Element[] = this._productModel.getLotSizeOperation(_productCodeElements[i].nodeValue, _processCodeElements[j].nodeValue);
                let _machinesRefElements: Element[] = this._productModel.getResourceMachineProcess(_processCodeElements[j].nodeValue);
                for (let k = 0; k < _machinesRefElements.length - 1; k++) {

                    matrixQuantityProduct[_machinesRefElements[k].nodeValue][_machinesRefElements[k + 1].nodeValue] += _quantityProduct; [line 96]
                    //matrixQuantityLotSizeProduct[][] += (_quantityProduct /parseInt(_lotSizeOperation[k].nodeValue)) ;

                }
            }
        }
        this.listMatrixLog(matrixQuantityProduct, matrixQuantityLotSizeProduct);

        this._matrixQuantityProduct = matrixQuantityProduct;
    }

    private listMatrixLog(matrixQuantityProduct: Array<Array<number>>, matrixQuantityLotSizeProduct: Array<Array<number>>) {
        for (let i = 0; i < this._machines.length; i++) {
            for (let j = 0; j < this._machines.length; j++) {
                this._log.trace("Machine/listMatrix - Quantity Matrixf : " + matrixQuantityProduct[this._machines[i].code][this._machines[j].code]); [line 110]
                this._log.trace("matrice quantityLotSize indice i = " + this._machines[i].code + " indice j = " + this._machines[j].code + " ,contient: " + matrixQuantityLotSizeProduct[this._machines[i].code][this._machines[j].code]); [line 111]
            }
        }
    }


    let cylinder = BABYLON.Mesh.CreateCylinder("cylinder", distance, this._matrixQuantityProduct[machineA][machineB] / 50, this._matrixQuantityProduct[machineA][machineB] / 50, 36, 1, this._scene, true); [line 220]

回答by Amid

It looks like you have some logical problems inside your code. Assuming that codeproperty is of type string, the following lines does not play nice together:

看起来您的代码中有一些逻辑问题。假设code属性是字符串类型,以下几行不能很好地搭配在一起:

let matrixQuantityProduct2: Array<any> = [];
matrixQuantityProduct2[this._machines[j].code] = 0;

Lets say this._machines[j].codeevaluates to '123'.

让我们说this._machines[j].code评估为'123'。

So you define variable of type array and try to access its element by string key '123'. In such case you will get value of the property 123on matrixQuantityProduct2object. That is probably not there - its an array after all?

因此,您定义数组类型的变量并尝试通过字符串键“123”访问其元素。在这种情况下,您会获得属性的值123matrixQuantityProduct2对象。那可能不存在 - 毕竟它是一个数组?

Most likely you would like to use matrixQuantityProduct2as a dictionary. In such case you can have it like this:

您很可能想matrixQuantityProduct2用作字典。在这种情况下,您可以像这样:

interface INameToValueMap
{
    [key: string]: any;
}

let matrixQuantityProduct3: INameToValueMap = {};
matrixQuantityProduct3["123"] = 0;

Note initialization with {}instead of [].

注意用{}而不是初始化[]

As a side note. You can disable such errors by setting "noImplicitAny": falsein your tsconfig.json compilerOptionssection. But I would not recommend doing this.

作为旁注。您可以通过"noImplicitAny": false在 tsconfig.jsoncompilerOptions部分中进行设置来禁用此类错误。但我不建议这样做。

Hope this helps.

希望这可以帮助。

回答by Jonatan Alama

About line 96, "nodeValue" seems to return type string, so try to use parseInt() in order to change it to a number.

关于第 96 行,“nodeValue”似乎返回类型字符串,因此尝试使用 parseInt() 将其更改为数字。

matrixQuantityProduct[parseInt(_machinesRefElements[k].nodeValue)][parseInt(_machinesRefElements[k + 1].nodeValue)] += _quantityProduct;

About the others, I do not get any errors by just trying your code, but it might be that you are creating Array<any>and trying to save it into Array<Array<number>>

关于其他人,我只是尝试您的代码并没有得到任何错误,但可能是您正在创建Array<any>并尝试将其保存到Array<Array<number>>