tslint prefer-const warning when using dot notation
Asked Answered
C

1

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

Contumely answered 6/4, 2018 at 22:31 Comment(2)
It is related to eslint. For a better understanding of this, head over to eslint.org/docs/rules/prefer-const. I hope it helps.Tokyo
I get a warning with snack.Wilds
W
4

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.

Wilds answered 6/4, 2018 at 23:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.