36

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
  • 26,597
  • 4
  • 30
  • 62
JITTU VARGHESE
  • 435
  • 1
  • 4
  • 6
  • 3
    It's because you overwrite your `'Mark'` value with `5`, so there is no reason to set it to `'Mark'` in the first place. – Dai Dec 20 '20 at 11:52
  • For Dai, even if I remove the line (name = 5), it still says deprecated. – JITTU VARGHESE Dec 20 '20 at 11:54
  • @Ivar That duplicate doesn't apply to `let name`, only `var name`. – Bergi Dec 20 '20 at 12:18
  • Doesn't vscode show an explanation for the deprecation? – Bergi Dec 20 '20 at 12:20
  • yeah, it happens everytime you call a property "name" (globally?) somewhere. the deprecation warning is from typscript for lib.dom.d.ts. Confused me as well when I saw it for the first time. – Welcor Mar 26 '23 at 15:33

3 Answers3

68

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
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 4
    Is that an IIFE? And if so, what does the exclamation mark at the beginning do. Does it like negate it or something? – theProCoder Oct 01 '21 at 11:36
  • is an IIFE, and the ! sign is only for small optimization. It doesn't matter at all. – Ale DC Feb 22 '22 at 15:54
  • For clarification, the ! is not an optimization in this example. For an IIFE, you can turn the function into an expression by wrapping in parentheses and invoking, or to save a few characters, by preceding with an operator and invoking. In the screenshot provided, the author has done both, making one or the other unnecessary. – Matthew Dean Sep 21 '22 at 15:51
1

Just to clarify, whenever something is striked out, its a warning that it basically used a lot and might be interfered with. Its just a warning for variables with basic names. You don't need to worry about it

-8

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
  • 339
  • 2
  • 4
  • 13
    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