7
votes
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?

1
It is related to eslint. For a better understanding of this, head over to eslint.org/docs/rules/prefer-const. I hope it helps.ParthS007
I get a warning with snack.David Sherret

1 Answers

4
votes

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.