웹사이트 검색

typescript - 빌드: 요소가 암시적으로 'any' 타입을 가지는데, 이유는 인덱스 표현식이 'number' 타입이 아니기 때문입니다


우리는 TypeScript 1.8 코드를 트랜스파일하고 있으며, 소프트웨어 개발자가 타입을 설정하도록 강제하고 있습니다.

다음은 오류 메시지입니다:

  • 설명: 빌드: 요소는 인덱스 표현식이 'number' 타입이 아니기 때문에 암시적으로 'any' 타입을 가집니다.
  • 프로젝트 myProject
  • 파일: C:\dev\Scripts\machines.ts
  • 줄: 77, 78, 80, 81, 96, 110, 111, 220
  • 출처: 빌드

질문

그 변수들의 유형을 어떻게 정의해야 합니까?

  • _행렬수량곱
  • _행렬수량로트사이즈제품
  • 행렬수곱2
  • 매트릭스수량롯사이즈제품2

소스 코드

    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]

답변 #1

코드 안에 논리적인 문제가 있는 것 같습니다. 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로 설정하면 이러한 오류를 비활성화할 수 있습니다. 하지만 저는 이렇게 하는 것을 권장하지 않습니다.

도움이 되길 바랍니다.

관련기사