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

Async Event Handlers are broken in TS files #4220

Closed
dev-nicolaos opened this issue Mar 2, 2020 · 3 comments · Fixed by #4240
Closed

Async Event Handlers are broken in TS files #4220

dev-nicolaos opened this issue Mar 2, 2020 · 3 comments · Fixed by #4240
Labels
bug Something isn't working correctly

Comments

@dev-nicolaos
Copy link
Contributor

dev-nicolaos commented Mar 2, 2020

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

@ry ry added the bug Something isn't working correctly label Mar 2, 2020
@kevinkassimo
Copy link
Contributor

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.

@kitsonk
Copy link
Contributor

kitsonk commented Mar 3, 2020

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;

@kevinkassimo
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants