Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tslint prefer-const warning when using dot notation

interface obj {
  bar: string
}

function randomFunction() {
  let foo: obj = { bar: "" }
  foo.bar = "hip"
}

let snack: obj = { bar: "" }
snack.bar = "hop"

I get this warning from tslint:

Identifier 'foo' is never reassigned; use 'const' instead of 'let'. (prefer-const)

Funny though I don't get this warning in the second case with the variable snack.

I can get rid of this warning (which clutters my console when transcompiling) with /* tslint:disable: prefer-const */

I haven't found any bug report on the tslint project. Since I'm new to typescript I'm wondering: Do I something wrong here?

like image 619
stackovermat Avatar asked Apr 06 '18 22:04

stackovermat


1 Answers

tslint is asking you to change let to const because the identifier foo is not reassigned.

The error can be removed by instead writing const:

const foo: obj = { bar: "" };
foo.bar = "hip";

Note that the const modifier just means you can't reassign the identifier:

 const foo = { bar: "" };
 foo = { bar: "" }; // error

It doesn't make the object itself readonly.

like image 187
David Sherret Avatar answered Sep 28 '22 06:09

David Sherret