DispatchEvent The reason for the error was the failure to execute "dispatchEvent" in the "EventTarget": parameter 1 is not of the type "Event",

I am using Angular2 + TypeScript and when trying to execute this code:

var event = new Event('check'); window.dispatchEvent(event); 

This code causes the following error:

failed to execute "dispatchEvent" in "EventTarget": parameter 1 is not of type "Event".

I tried everything and found out that this would work:

 var event = new CustomEvent('check'); 

The problem is that CustomEvent is not supported by any IE.

Other that works:

 var event = new Event('CustomEvent'); event.initCustomEvent('check'); 

The problem is that InitCustomEvent is deprecated.

Is there anything I can do to avoid this type of error? Maybe make an exception somewhere in typescript? Or use any code that is not outdated and works for IE Edge?

+4
javascript angular typescript
source share
1 answer

Found a solution.

For some TypeScript parameters, a new event is of type Event only if we insert the second parameter as an example:

 var ev = new Event("check", {"bubbles":true, "cancelable":false}); window.dispatchEvent(event); 
+3
source share

All Articles