2016-04-04 13 views
4

TypeScript 1.8コードを翻訳してソフトウェア開発者に型を設定させようとしています。ここでビルド:要素が暗黙的に 'any'型を持っています。インデックス式の型が 'number'ではないためです。

はエラーメッセージです:

  • 説明:ビルド:インデックス式はタイプ「数」がないので、要素が暗黙のうちに「あらゆる」タイプがあります。
  • プロジェクトMYPROJECT
  • ファイル:77、78、80、81、96、110、111、220
  • ソース:
ビルドC:machines.ts
  • 行\ \ DEV \スクリプト

    質問

    これらの変数の種類はどのように定義する必要がありますか?

    • _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] 
    
  • 答えて

    14

    にそれを保存しようとしているあなたには、いくつかの論理を持っているように見えていることかもしれませんコード内の問題codeプロパティはString型であると仮定すると、次の行は、一緒に素晴らしいプレーしていません。

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

    this._machines[j].codeが「123」と評価されたとしましょう。

    したがって、array型の変数を定義し、その要素に文字列キー '123'でアクセスしようとします。そのような場合にはmatrixQuantityProduct2オブジェクトの123プロパティの値を取得します。結局のところ、それはおそらくそこにはありません。

    ほとんどの場合、matrixQuantityProduct2を辞書に使用します。

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

    {}の代わり[]と注意の初期化:このような場合、あなたはこのようにそれを持つことができます。

    注釈として。このようなエラーを無効にするには、tsconfig.json compilerOptionsセクションに"noImplicitAny": falseを設定します。しかし、私はこれを行うことをお勧めしません。

    これが役に立ちます。

    0

    96行目については、「nodeValue」は型文字列を返すように見えるので、数値に変更するにはparseInt()を使用してみてください。他の人について

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

    、私はあなたのコードを試みることによって、すべてのエラーを得ることはありませんが、それはあなたがArray<any>を作成し、Array<Array<number>>

    +0

    ありがとう@jonatin質問に直接ではなく、可能な限り答えているあなたのコメントを見ていきます。素晴らしいフィードバックに感謝します! – Abdelkrim

    関連する問題