-
Notifications
You must be signed in to change notification settings - Fork 12.8k
js with window object fails to compile #10457
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
Comments
FWIW, my environment, I'm editing with Visual Studio Code, and running Ionic 2(beta) from the cli. Both the ionic build and Visual Studio threw compile errors. I'm not really sure what the error is exactly, so please edit the title as appropriate! |
@ChogyDan the simplest fix is to annotate what the parameter function storageAvailable(type: 'localStorage' | 'sessionStorage') {
try {
var storage = window[type];
var x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch(e) {
return false;
}
} |
tsc --version |
@DanielRosenwasser the version I posted does work fine and compiles too. The only difference is that Typing |
Version updated, thanks. I also appreciate the recommended fixes for the code. @DanielRosenwasser is interesting because both But for the sake of argument, explicitly typing |
The surprising part as @ChogyDan mentions is this: let a;
let b = window[a]; // type of b is 'Window' !? This arises from the interface Window {
...
[index: number]: Window;
} I was going to suggest adding |
i would say remove the indexer from |
@mhegazy I looked up the indexer in the spec, and it looks like it is supporting I thought removing the indexer might be a good idea, but I guess it won't follow the spec. References: |
I wrote that specific code snippet on Mozilla.com. I have to admit I have never used TypeScript before. My idea for the snippet was that people should be able to easily copy-paste and use it... So I'm sorry to hear that it's giving issues. If you have any suggestions for how to change that snippet so it would work out-of-the-box in both TypeScript and Javascript then please share them here. |
i do not think there is something wrong with the snippet per se. the issue here is the DOM spec says that indexing into the My proposal is to remove the index signature all together. I would expect ppl indexing into |
Are we sure this isn't an issue with typescript itself? It seems that indexing with type
|
|
@mhegazy I don't think there is one correct thing to do here. The current logic favours arrays over objects, leading to surprises like this issue. I was expecting similar behaviour to how TypeScript handles overloads - i.e. declaration order matters. The following example is very similar to this issue except the indexers are overloaded functions. In this case, calling interface Window {
indexer(string: string): any;
indexer(number: number): Window;
}
let w: Window;
let prop: any;
// If we swap the overloads above, a would be Window
let a = window.indexer(prop); // a is any (uses first overload)
let b = window.indexer('foo'); // b is any (regardless of overload order)
let c = window.indexer(42); // c is Window (regardless of overload order) |
that is correct. except that non-array objects rarely have numeric indexers. |
So in this specific case, would changing |
yes. But ideally you would not need to do this. |
I was waiting for the "TypeScript gods" to make a declaration of how to resolve this dilemma: does TS need to change to accommodate more precise indexer definitions? or should the the .d.ts file be adjusted? I realized that unless there are other abstractions that TS is trying to model that need complicated indexers, it shouldn't be changed. The window abstraction doesn't need to be the driver for this change. So removing the indexer seems like the right call. I went ahead and researched how to do a pull request for this issue. You can see my work here: microsoft/TypeScript-DOM-lib-generator#146 |
TypeScript Version: Version 2.0.0
Code
Expected behavior:
This is a snippet of code from: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
Since this is sample javascript code, I was expecting it to work.
Actual behavior:
var storage gets typed as Window, and thus can't resolve setItem nor removeItem.
The text was updated successfully, but these errors were encountered: