Skip to content

Add ability to extend/replace built-in constructor declarations (eg. CustomEvent) #435

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

Closed
DanTup opened this issue Aug 12, 2014 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@DanTup
Copy link

DanTup commented Aug 12, 2014

I raised this on Codeplex; but I don't believe it's supported. I'm not sure which of the two referenced cases are most appropriate, so thought it best to raise as I'd posted originally. If this overlaps with an existing GH issue; please let me know.

Original Codeplex issue: http://typescript.codeplex.com/workitem/2531

I'm using the CustomEvent polyfill from here:
https://developer.mozilla.org/en/docs/Web/API/CustomEvent

DOM4 specifies a CustomError constructor that takes a string type and an (optional) custom data object:

window.dispatchEvent(new CustomEvent('doStuff', { detail: 1 }));

It seems that TypeScript will not allow this code to be compiled, it expects an argument-free constructor. I cannot figure out how to extend/replace CustomEvent to allow it. There's an answer on SO that seems to explain why:

http://stackoverflow.com/questions/17571982/dispatching-custom-events

it says:

If the CustomEvent definition was purely an interface, you would be able to extend it thus:
(blah)
But the CustomEvent constructor is actually defined in a variable declaration, which you can't extend

My codebase is currently only around 40 lines of JavaScript, and already I can't make it work in TypeScript :(

I think TypeScript needs to be able to support things like this; Browsers will never have identical implementations and the specs are moving targets. We need to not have our hands tied by decelerations in lib.d.ts with the only way around them being to have a local hacked copy of lib.d.ts!

Note: I actually think the current CustomEvent decleration in TypeScript is kinda useless because Chrome doesn't even allow you to call "new CustomEvent()"; it'll crash. Even removing it completely and forcing me to declare it seems a better option than what we have now :-/

@DanTup
Copy link
Author

DanTup commented Aug 12, 2014

Oh; just found #182; this case might be considered a dupe of that?

@RyanCavanaugh
Copy link
Member

Yea, I think fixing #182 would fix this issue (assuming we do it broadly, which I think we would)

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 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