-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Variable usage issues caused by global Window object #1351
Comments
I figured out why this happens - and I think the TypeScript compiler needs, at the very least, a warning here. Apparently, all the global Window properties are visible "as is" - which means that using them inside functions is automatically "resolved", even if their use is a typo or an error (as it was in my case). If I may suggest, please add a warning when compiling .ts code that uses these "globals" - e.g.: ... bug.ts(6, 10): Warning: you are accessing the global 'window.name' here - to remove this warning, change 'name' to 'window.name'. |
We don't actually add all the members of Window to the global namespace; I am inclined to say we should remove the |
👍 Other common sources of error include I've also had problems with |
I don't think it should be removed. That would allow writing code like var name = [1, 2, 3]; // Expect name to be number[]
name.forEach(...); // Blows up
console.log(name === "1,2,3"); // true - it's actually the string "1,2,3" because it does set window.name which stringifies the array Edit: Or even with a string |
With the library modularization work at #6974, we may have an option for |
node users can ignore the DOM, but web developers will still run into these inadvertently. |
About window.event I think it's not really same, as topic starter's problem — name property is standard. Yes, it can also lead to misunderstandings, but “present in many environments” is not seems to me as an argument — it's non-standard property from old times. In such case you can add attachEvent function, why not? There is a lot of really useful properties, that didn't exist in TypeScript definitions. For example, when I need to control css-property declare global
{
interface CSSStyleDeclaration
{
resize?: string;
}
} If somebody really wants to use I'm sorry if my message will seem harsh, but it seemed to me that TypeScript tends to follow the standards, not to describe some specific implementation. This is not a complaint, just my thoughts. And thanks for your work — TypeScript is a great language! |
The solution we've gone with is to specify |
Should |
I agree with @Seikho. ECMAScript is host-neutral. W3C specifies web APIs for standard browser hosts, The Node.js Foundation specifies the node.js host environment. I don't think either belong in the default The OP's problem arises because the default I'd prefer that the browser host environment was in a separate opt-in The team recently decided not to bundle node.js typings. How about unbundling the web API typings too? That would make TypeScript host-neutral like ECMAScript is, and put node and browser projects on an equal footing. |
@yuit can you confirm @Arnavion's above comment? Looking at the diff for 6974, I can see that How do I target, say, an ES6 node.js environment, without including all the browser-side typings? |
Confirmed it myself. Adding Adding So, is there any reason why this issue remains open? It seems no longer a problem. What other changes are needed? EDIT I'd say discoverability is still an issue. |
@yortus the "solution" to this thread is not the |
And I once again draw attention to what is now the library has an error — property |
The problem with global 'name' is going to be fixed (#9850). |
@mhegazy - This issue as stated in the original post is not fixed. I can still do Even worse, I can now do stuff like this:
|
|
@mhegazy I'm not sure what you meant by "remove it totally". My understanding is that you are saying we cannot remove However, we could argue that typing Basically, using So we can do better. |
The example below compiles - it shouldn't, I think...
Typescript fails to see that there is no "name" variable - only "config.name" is valid in this context.
I am using the version from the repos, commit af4a121 .
The text was updated successfully, but these errors were encountered: