-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dangerous "name" (and potentially others) global #18433
Dangerous "name" (and potentially others) global #18433
Comments
Duplicate of #9850 |
that didn't fix the problem. you can still use the variable and the nightly will happily use it without complaining, so not really a duplicate, but a regression the reason for an opt-in is because some typings need "DOM" declarations (like pouchdb), and even if you're using node, you need to use "lib.dom", just because you need the |
We keep going around on this and need to come up with something that really solves the issue |
Spoke with @mhegazy - let's change it to |
how about a tiny breaking change for 2.6 and separate the global DOM declarations into another lib file? is it feasible? |
u can do this today with |
but I have typings that rely on DOM stuff, even if it's not actually used in node.js (like pouchdb), but there are many other isomorphic typings |
Well then you include the DOM library... It doesn't make any sense to have a partial DOM library, or even a globals DOM library... Because I am assuming you aren't suggesting that the global |
properties of "window" that are considered globals is there, under |
Not using them doesn't make them go away... So you are suggesting that the libraries not reflect the actual environment? |
not sure if trolling or serious. |
You implied breaking out the DOM definitions. Mohammed told you how you can do that today. You then implied that wasn't good enough and seemed to imply something else, like that the global you don't like be located in a separate library file. Why do that, or more specifically what are you suggesting, because it appears to make no sense to me. |
|
Can you remember #15424? And I guess |
|
Should it be Aside from anything, it sounds REALLY cool — might sell some T-shirts and other memorabilia. |
Certainly |
@falsandtru the global Signalling up both |
|
A patch exists here: microsoft/TypeScript-DOM-lib-generator#240 |
Simple suggestion: remove function use(obj) {
ñame = obj.name;
~~~~
Cannot find name 'ñame'.
} Declare |
Besides, look at this misleading error you get in current TS: myname = 'Allen'
~~~~~~
Cannot find name 'myname'. Did you mean 'name'? The existing declaration trickery pushes people into using |
@mihailik insufficient because you can still do this
|
just got bitten again by "status" (that comes from |
I'll add my voice to the chorus here. There's a line in my code that's used to fetch icon objects based on icon names:
where Took me 10 minutes to trace it back, and then another 10 minutes of googling to find out why my compiler hadn't noisily failed/to find this thread. |
PS: this issue had been open since 2017, it's now 2020, even if someone accidentally used the variable in history, it should be all faded out by now. PPS: new versions of TypeScript did break some compatibilities, for instance, the IterableIterator interface, I don't see anybody complaining about it, people just think TypeScript is getting better and more accurate. |
I've just been bitten by this, and I'm not even writing TS, I'm using VSCode's type-checking support for plain JS. I was very suprised to find |
The current functionality has allowed some bugs in our codebase. "name", "length" & "event" being global variables seems like ancient technology to me. Is anybody using TypeScript and still accessing these? Here's our workaround using an ESLint rule: |
This just got me in a node project. |
Experienced dev new to Typescript, just bit by this. I have no position on how this should be improved, but the current behavior is a bad dev experience to the point where it should absolutely be changed. Names like, well, "name", are extremely common; it's a massive footgun for code to compile successfully and then error at runtime due to what is substantially an undefined variable, even if this global type definition makes sense in the historical context. |
If anyone would argue that changing this may lead to unexpected errors for historical projects, I'd like to say that those projects are buggy by themselves. |
With PR #40402, we can type those variables as |
#40468 but it requires changes in |
@sandersn this shouldn't be closed, there are many globals that should be changed |
I'm cool with keeping it open (it auto-closed) for discussion about the rest of the values, |
For anyone interested, discussion on ideas for addressing globals in lib.dom is the main topic of discussion in #14306 :) |
Thanks for the pointer @david-fong . IMHO this issue should be closed in favor of that one, or, frankly, both should be closed as wont-fix. I think the eslint rule fully solves the actual problem, which is that implicit Look at discussions on other issues that want to catch code that has a bad smell but isn't actually wrong, like #13778. When you add a check like this to the compiler, you're going to either break a lot of code, or put it behind a disabled-by-default flag. New flags will cause a split, where some packages were compiled with it turned off, so now consuming code has to either bug maintainers to change their projects to comply with a flag they didn't ask for, or turn on tl;dr: this is better addressed by a linter rule, which is already available. |
Those are great points. I didn't think of that. I don't think the lib.dom-strict option mentioned in that discussion would suffer from those problems though- would they? What I like about it is that it seems like it would be very simple to implement in the ts lib generator. I think it would be great if you paste your comment into #14306 along with your thoughts on the lib.dom-strict option. |
I'm not an expert but I think if you fork |
Let's continue this discussion in 14306. |
TypeScript Version: 2.6.0-dev.20170913
Code
https://www.typescriptlang.org/play/#src=if (name %3D%3D%3D "asdf") {%0D%0A console.log('hello')%0D%0A}
Expected behavior:
Error out
Actual behavior:
More than once, I had a "local" variable called
name
, outside a block scope, and it didn't warn me that it's not defined (or used before declaration). even though, in my code,event.name
is a string, the globalname
variable is of typenever
. it's defined inlib.dom.d.ts
, along with other "juicy" global names that will provide some confusion, likelength
,external
,event
,closed
one 'fix' (that would be a breaking change) would to make
lib.dom-globals.d.ts
(the propertiers that are usually accessible throughwindow
variable) and make it an opt-in intsconfig.json compilerOptions.lib
arrayThe text was updated successfully, but these errors were encountered: