【问题标题】:error in typescript for each loop syntax每个循环语法的打字稿错误
【发布时间】:2017-09-29 19:46:01
【问题描述】:

我正在尝试使用 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);
  }
}

在cloneCar方法中,我在尝试写入时出现这个错误:

for (let  prop: string in c) {
...}

Tslint:标识符 'prop' 永远不会重新分配,使用 const 代替 让

这是从我的 IDE 捕获的图像: see error here

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

请帮忙!

【问题讨论】:

  • 这只是一个 linting 错误,它告诉你使用 const 而不是 let in ,因为你永远不会重新评估 prop 到另一个值。
  • 还是有问题,是不是打字稿版本不对?
  • 不,这是 tslint 给你的风格指示.. palantir.github.io/tslint
  • 遵循这个规则是好的。它使代码更易于阅读和维护。
  • 是的,我还在寻找解决方案

标签: javascript angular typescript


【解决方案1】:

您的 IDE 是正确的。您将 prop 声明为 let 而不是 const

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

let x = 5;
x = 7;

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

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

const x = 5;
x = 7; // Throws an error.

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

for (const  prop: string in c) {
...}

查看this documentation 以获得更深入的了解。

【讨论】:

  • 请注意,IDE 和编译器很少出错...除了 XCode :D
  • @n00dl3 哈哈哈!是的!您还可以决定将哪些内容显示为错误。所以也取决于你如何配置你的 IDE。无论如何,总是对产生更好的代码有很大的帮助。 IDE 的更正对我帮助很大... :D
【解决方案2】:

首先,它只是 linting 错误,告诉您如果不重新分配变量,最好使用 const 而不是 let

第二个是我会选择Object.assign(),而不是它,但它是单线的。

【讨论】:

  • Object.assign 更简洁的方法是使用{...}
【解决方案3】:

变量 prop 仅用于读取。你没有给它分配任何东西,所以它不会改变。这意味着它应该是 const not let。

【讨论】:

    猜你喜欢
    • 2021-08-20
    • 1970-01-01
    • 2018-12-20
    • 2019-10-26
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    相关资源
    最近更新 更多