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
|
something weird happened just now, I had an event called "onSelect", but typescript accepted the global "onselect" (from window.onselect). another one that bit me. it's "null" by default, and my props were |
Perhaps we could get a compiler flag for |
Forgot to destruct my variable 'name' from props and ended up using the global name variable instead. To me it's obviously a very bad idea to have globals with such common variable names as 'name' and 'length'. Any plans on removing them and letting people access them through window instead? Or have they been already in later versions (I'm using v2.8.3) |
the issue is much bigger than it appears to be, since it can ship broken software. even with all the strict functions enabled, and using tslint as well... |
This is an obvious BUG, admit it |
I agree with this being a bug, even with // ------- 1 -------
interface Foo {
foo: object;
}
const foo: Foo = {
foo: name
};
// ------- 2 -------
function a(foo: string) {}
a(name);
// ------- 3 -------
function b(params: {foo: string}) {}
b({ foo: name }); All 3 compiles perfectly. All 3 should be compile-time errors instead (not linting errors!) because they are directly related to symbol/type resolution (which is compiler's domain). |
I am still unsure why unknown wouldn’t be a better type than never. |
this issue missed the timing for a breaking change before wide adoption of TS... we have |
Same on this one🤦🏻♂️ |
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, |
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: