Deno: Async Event Handlers are broken in TS files

Created on 2 Mar 2020  Â·  3Comments  Â·  Source: denoland/deno

Environment

  • Windows 10 Home version 1909
  • Deno version 0.35.0

Steps to Reproduce

  1. Create a file test.ts
window.addEventListener("load", async () => {
  console.log("hello");
});
  1. Run the file: deno test.ts

Expected Behavior

The program prints hello, then exits with no errors.

Actual Behavior

The following error is printed to the console and the program exits without printing hello:

error TS2345: Argument of type '() => Promise<void>' is not assignable to parameter of type '(event: Event) => void | null'.
  Type 'Promise<void>' is not assignable to type 'void'.

â–º file:///Path/To/CWD/test.ts:1:33

1 window.addEventListener("load", async () => {

Notes

bug

All 3 comments

I think marking the return types of callbacks to be any might be reasonable here.

I also noticed that addEventListener should also be able to take objects implementing EventListener interface, not just plain functions.

We really should follow the TypeScript typings for stuff like this as they get a lot of more real world use. TypeScript has:

addEventListener<K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

xueqingxiao picture xueqingxiao  Â·  3Comments

davidbarratt picture davidbarratt  Â·  3Comments

justjavac picture justjavac  Â·  3Comments

ry picture ry  Â·  3Comments

CruxCv picture CruxCv  Â·  3Comments