【问题标题】:Build: element implicitly has an 'any' type because index expression is not of type 'number'构建:元素隐式具有“任何”类型,因为索引表达式不是“数字”类型
【发布时间】:2016-07-23 17:58:37
【问题描述】:

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

以下是错误消息:

  • 描述:构建:元素隐含地具有“任何”类型,因为索引表达式不是“数字”类型。
  • 我的项目项目
  • 文件:C:\dev\Scripts\machines.ts
  • 线路:77、78、80、81、96、110、111、220
  • 来源:构建

问题

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

  • _matrixQuantityProduct
  • _matrixQuantityLotSizeProduct
  • matrixQuantityProduct2
  • matrixQuantityLotSizeProduct2

源代码

    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]

【问题讨论】:

    标签: typescript typescript1.8


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      您的代码中似乎存在一些逻辑问题。假设code 属性是字符串类型,以下几行不能很好地结合在一起:

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

      假设this._machines[j].code 的计算结果为“123”。

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

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

      interface INameToValueMap
      {
          [key: string]: any;
      }
      
      let matrixQuantityProduct3: INameToValueMap = {};
      matrixQuantityProduct3["123"] = 0;
      

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

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

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2018-04-25
        • 2023-04-01
        • 2021-10-11
        • 2017-03-14
        • 2021-08-01
        • 1970-01-01
        • 2023-02-15
        • 2022-06-12
        • 2021-12-20
        相关资源
        最近更新 更多