5

I'm trying to make a class with typescript 2.0.3 but I have some problems and I don't know why.

this is my code

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);
  }
}

In the cloneCar method, I have this error when trying to write:

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

Tslint: identifier 'prop' is never reassigned ,use const instead of let

this is an image capture from my IDE: see error here

NB: I'm using this code in angular project version 2.3.0

Some help, please!

5
  • 1
    it's just a linting error, it's telling you to use const instead of let in that for because you are never reassining prop to another value. Commented May 2, 2017 at 10:14
  • still given problem , is that problem caused by the wrong version of typescript ? Commented May 2, 2017 at 14:27
  • no, it's a style indication tslint gives you.. palantir.github.io/tslint Commented May 2, 2017 at 18:17
  • 1
    It is good to follow this rule. It makes code easier to read and maintain. Commented May 3, 2017 at 3:19
  • yes i do , i 'm still searching for solution Commented May 3, 2017 at 7:42

3 Answers 3

4

Your IDE is right. You are declaring prop with let and not with const.

let is for variables that are subject to change. For example:

let x = 5;
x = 7;

We declared x and then we changed it's value.

const is for values that won't change. For example:

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

So, in your case, as prop doesn't and won't change, has to be a constant (const):

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

Check this documentation for a deeper understanding.

Sign up to request clarification or add additional context in comments.

2 Comments

Note that IDE and compilers are rarely wrong... With the exception of XCode :D
@n00dl3 hahaha! Yeah! Also you can decide what to show as error or not. So depends also on how you configured your IDE. Anyhow, are always a great help to produce a better code. IDE's corrections helped me a lot tbh... :D
0

variable prop is only used to read. you do not assign anything to it so it doesn't change. this means it should be const not let.

Comments

0

1st of all it is just linting error telling you that it is better to use const instead of let if you are not reassigning variable.

2nd is that I would go with Object.assign() instead which does the same but is one-liner.

1 Comment

An even cleaner approach than Object.assign would be to use {...}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.