Skip to content
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

Generalising the Event interface #7483

Closed
rtpg opened this issue Mar 11, 2016 · 1 comment
Closed

Generalising the Event interface #7483

rtpg opened this issue Mar 11, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@rtpg
Copy link

rtpg commented Mar 11, 2016

Been having an issue with this snippet of code:

var reader = new FileReader();
var debug = {hello: "world"};
var blob = new Blob([JSON.stringify(debug, null, 2)], {type : 'application/json'});

reader.onload = function(evt){
  console.log(evt.target.result);
}
reader.readAsText(blob);

So when running this as plain JS, I end up logging {hello:"world"}. But this doesn't typecheck right now.

So currently the interface structures involved in lib.d.ts are something like (trimmed for space):

interface Event {
    target:EventTarget;
}

interface FileReader  extends EventTarget{
    onload:(ev:Event)=>any;
    result: any;
}

The interface signature makes it clear that FileReader is meant to be the target, but because of how the interfaces are structured, this doesn't bubble down properly.

In a vacuum, the simple solution seems to be to make Event be generic on its target:

interface Event<T extends EventTarget>{
    target:T
}

interface FileReader extends EventTarget{
    onload:(ev:Event<FileReader>)=>any;
    result: any;
}

But two things cause me to hesitate at this patch:

  • The Event interface is defined in the W3C DOM spec .I'm not quite sure what the policy is upstream with Edge on this, but introducing generics into the typing might be too much deviation from this.
  • Event touches a lot of stuff, so there's a lot of collateral damage. I think it would require rewriting almost everything related to Events (kind of defeating the purpose of generating lib.d.ts from Edge).

As a side note, I've noticed that there's a lot of onX(ev:Event<Self>)=>any-type event listeners a bit all over the place (where Self is the containing interface). I run into this quite a bit in my own code, I wonder if there's value in having a special type variable to refer to the containing class.

Anyways, I wanted to get some feedback on this before moving forward.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 29, 2016

looks like a duplicate of #299

@mhegazy mhegazy closed this as completed Mar 29, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label Mar 29, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants