Abd*_*rim 13 typescript typescript1.8
我们正在编写TypeScript 1.8代码,迫使软件开发人员设置类型.
以下是错误消息:
题
我们应该如何定义这些变量的类型?
源代码
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]
Run Code Online (Sandbox Code Playgroud)
Ami*_*mid 29
看起来您的代码中存在一些逻辑问题.假设code属性是字符串类型,以下行不能很好地一起使用:
let matrixQuantityProduct2: Array<any> = [];
matrixQuantityProduct2[this._machines[j].code] = 0;
Run Code Online (Sandbox Code Playgroud)
让我们说this._machines[j].code评价为'123'.
因此,您定义类型数组的变量,并尝试通过字符串键'123'访问其元素.在这种情况下,您会获得属性的值123上matrixQuantityProduct2对象.那可能不存在 - 毕竟它是一个阵列?
很可能你想matrixQuantityProduct2用作字典.在这种情况下你可以像这样:
interface INameToValueMap
{
[key: string]: any;
}
let matrixQuantityProduct3: INameToValueMap = {};
matrixQuantityProduct3["123"] = 0;
Run Code Online (Sandbox Code Playgroud)
注意初始化{}而不是[].
作为旁注.您可以通过"noImplicitAny": false在tsconfig.json compilerOptions部分中进行设置来禁用此类错误.但我不建议这样做.
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
38462 次 |
| 最近记录: |