daghan
daghan

Reputation: 1028

new Event('myEvent') does not compile in typescript

https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events

I am trying to create a custom event in typescript like above example:

var ev: Event = new Event('myEvent');

Sadly, in lib.d.ts, new for Event is parameterless:

declare var Event: {
    prototype: Event;
    new(): Event; // NO PARAMETERS!
    CAPTURING_PHASE: number;
    AT_TARGET: number;
    BUBBLING_PHASE: number;
}

Thus the constructor with a string parameter (event name) does not compile.

Any suggestions?

Upvotes: 2

Views: 1549

Answers (1)

PSL
PSL

Reputation: 123739

Most possibly you might be having an older version of typescript installed with its specific library files. The definition of Event is this way.

declare var Event: {
    prototype: Event;
    new(type: string, eventInitDict?: EventInit): Event;
    AT_TARGET: number;
    BUBBLING_PHASE: number;
    CAPTURING_PHASE: number;
}

I am not quite sure, but i believe typescript variables are not partials unlike interfaces. You could upgrade TS version, update lib.d.ts manually to edit the type or create a new type and use it.

Upvotes: 2

Related Questions