每个循环语法的typescript错误

Moh*_*BLI 5 javascript typescript angular

我正在尝试使用typescript 2.0.3创建一个类但我有一些问题,我不知道为什么.

这是我的代码

import { Component, OnInit } from '@angular/core'; 
import {Car} from '../interfaces/car';

class PrimeCar implements Car 
{
  constructor(public vin, public year, public brand, public color) {}
}
@Component({
  selector: 'rb-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
  displayDialog: boolean;
  car: Car = new PrimeCar(null, null, null , null);
  selectedCar: Car;
  newCar: boolean;
  cars: Car[];
  constructor() { }
  ngOnInit() {
this.cars = [ {vin: '111', year: '5554' , brand: '5646' , color: '6466' },
              {vin: '111', year: '5554' , brand: '5646' , color: '6466' },
              {vin: '111', year: '5554' , brand: '5646' , color: '6466' },
              {vin: '111', year: '5554' , brand: '5646' , color: '6466' }
             ];
  }    
  showDialogToAdd() {
    this.newCar = true;
    this.car = new PrimeCar(null, null, null, null);
    this.displayDialog = true;
  }
  save() {
    const cars = [...this.cars];
    if (this.newCar) {
      cars.push(this.car);
    } else {
      cars[this.findSelectedCarIndex()] = this.car;
    }
    this.cars = cars;
    this.car = null;
    this.displayDialog = false;
  }
  delete() {
    const index = this.findSelectedCarIndex();
    this.cars = this.cars.filter((val, i) => i !== index);
    this.car = null;
    this.displayDialog = false;
  }
  onRowSelect(event) {
    this.newCar = false;
    this.car = this.cloneCar(event.data);
    this.displayDialog = true;
  }    
  cloneCar(c: Car): Car {
    const car = new PrimeCar(null, null, null, null);
    for (let  prop: string in c) {
      car[prop] = c[prop];
    }
    return car;
  }
  findSelectedCarIndex(): number {
    return this.cars.indexOf(this.selectedCar);
  }
}
Run Code Online (Sandbox Code Playgroud)

在cloneCar方法中,我在尝试编写时遇到此错误:

for (let  prop: string in c) {
...}
Run Code Online (Sandbox Code Playgroud)

Tslint:标识符'prop'永远不会被重新分配,使用const而不是let

这是我的IDE中的图像捕获: 请在此处查看错误

注意:我在角度项目版本2.3.0中使用此代码

请帮忙!

SrA*_*Axi 4

你的IDE是对的。您声明的是propwith let,而不是 with const

let适用于可能发生变化的变量。例如:

let x = 5;
x = 7;
Run Code Online (Sandbox Code Playgroud)

我们声明了它x,然后我们改变了它的值。

const适用于不会改变的值。例如:

const x = 5;
x = 7; // Throws an error.
Run Code Online (Sandbox Code Playgroud)

因此,在您的情况下,因为prop不会也不会改变,所以必须是一个常量(const):

for (const  prop: string in c) {
...}
Run Code Online (Sandbox Code Playgroud)

查看此文档以获得更深入的理解。