23

My editor (VS Code) shows that my variable name is deprecated. The variable name is struck out from the 2nd line. Can you help?

let name = 'Mark';
name = 5;
console.log(name);
user3840170
  • 22,750
  • 2
  • 21
  • 50
JITTU VARGHESE
  • 305
  • 1
  • 2
  • 6

2 Answers2

46

In a browser, the global name variable has special meaning. This has caused people a lot of confusion over the years as they tried to create their own global variable named name and then found it coerced into a string.

The checker you are using doesn't appear able to special case an assignment to name if it follows a declaration of let name.

You can see that the message goes away if you put the code inside a function.

Screenshot demonstrating the above

Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
-3

In VS Code, find the variable name where it is showing deprecated name. Put your cursor and right click on it, then select 'Go to Definition'. It will open another file named as lib.dom.d.ts.

lib.dom.d.ts

Just remove the line "declare const name: void;"
This will remove your variable name from the deprecated list.

Mubtasim Fuad
  • 161
  • 1
  • 4
  • 6
    You are literally modifying the global typescript namespace. Anytime you upgrade typescript or do anything else, this might change back. Also some typescript code might depend on this. Absolutely not recommended. – leo848 Jan 04 '22 at 20:25