TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Variable of name, "name", is not validated

Open tlong-dev opened this issue 11 years ago • 3 comments

When a variable is named "name" (let name;) it is not checked for validity.

For example:

let s = {};
console.log(s[t]);

The tsc command fails on this code since t obviously doesn't exist. However, changing nothing else but the variable used in the object's index...

let s = {};
console.log(s[name]);

... tsc doesn't complain at all about this. To me this seems like a bug. Why are variables named name special?

Apologies if I missed an already open issue but due to the nature of the terms used to describe this issue it's kinda hard to search on.

tlong-dev avatar Nov 16 '15 19:11 tlong-dev

name is the global that you could also access via window.name (https://developer.mozilla.org/en-US/docs/Web/API/Window/name)

RyanCavanaugh avatar Nov 16 '15 19:11 RyanCavanaugh

See also https://github.com/Microsoft/TypeScript/issues/1351#issuecomment-106573083 for a good explanation of why it would be a bad idea to remove the global name declaration, as one might be tempted to do.

RyanCavanaugh avatar Nov 16 '15 19:11 RyanCavanaugh

I had a sneaking suspicion that it was falling back to a higher scope. Silly javascript. I tried the last suggestion in the thread you mentioned but it seemed to work fine. I'll comment there. Thank you once again @RyanCavanaugh!

tlong-dev avatar Nov 16 '15 19:11 tlong-dev