We are transpiling our TypeScript 1.8 code forcing the software developer to set the types.
Here are the error messages:
QUESTION
How should we define the types of those variables?
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]
The error "Element implicitly has 'any' type because index expression is not of type 'number'" occurs when an array is indexed with a value that is not a number. To solve the error, use an object if storing key-value pairs or use a type assertion.
The error "Element implicitly has an 'any' type because expression of type 'string' can't be used to index type" occurs when we use a string to index an object with specific keys. To solve the error, type the string as one of the object's keys.
It looks like you have some logical problems inside your code. Assuming that code
property is of type string, the following lines does not play nice together:
let matrixQuantityProduct2: Array<any> = [];
matrixQuantityProduct2[this._machines[j].code] = 0;
Lets say this._machines[j].code
evaluates to '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 123
on matrixQuantityProduct2
object. That is probably not there - its an array after all?
Most likely you would like to use matrixQuantityProduct2
as a dictionary. In such case you can have it like this:
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": false
in your tsconfig.json compilerOptions
section. But I would not recommend doing this.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With