You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms: Window, Worker, addEventListener, union types
Code
I have a function which can either accept a Window or a Worker - there are many similarities between the two - and I want to be able to use common methods without caring about which of the two types the argument is. This function works if the argument w is set to be either Window or Worker in isolation, but not if it's the union type.
Actual behavior: <"message"> part throws an error - Typescript seems to only want to use the non-generic version of the addEventListener function, which my example doesn't fit since MessageEvent cannot be cast to Event.
Search Terms: Window, Worker, addEventListener, union types
Code
I have a function which can either accept a
Window
or aWorker
- there are many similarities between the two - and I want to be able to use common methods without caring about which of the two types the argument is. This function works if the argumentw
is set to be eitherWindow
orWorker
in isolation, but not if it's the union type.Both
Window.addEventListener
andWorker.addEventListener
have an overloaded type definition of:Expected behavior:
No problems
Actual behavior:
<"message">
part throws an error - Typescript seems to only want to use the non-generic version of theaddEventListener
function, which my example doesn't fit sinceMessageEvent
cannot be cast toEvent
.Playground Link:
http://www.typescriptlang.org/play/#code/MYewdgzgLgBA7gRgFwwOoEswBMRxgXnkxzgG4AoUSWOAJhVRACcBrAUyYJjDb0dY4AKAOTCAlKRjlyAMwCuYYFHTgYAWwCeAMQXBBcBs3acAPmmK4xKAG4h0WGAG9yMVzCrQYACwCG2ADYcXIJqbBAQPgDmbCgAsmER0QCi1mxgUFYwfhoEAHxOHiCBAHT+IJEhCVFsYgC+FG7wxT5YWClpUAAy6NBpHAA8AESh4dWDuSIjiWzCADTefliBTBLktUA
The text was updated successfully, but these errors were encountered: