Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Can't read newURL property on Event in TypeScript

Hello there!

I am trying to catch the 'onHashChange' event in TypeScript and to work with the new URL. Here is the code:

JavaScript:
window.addEventListener('hashchange', function(event: HashChangeEvent) {
      console.log(event);
    });

However, the compiler gives this error:

Code:
No overload matches this call.
  Overload 1 of 2, '(type: "hashchange", listener: (this: Window, ev: Event) => any, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
    Argument of type '(this: Window, event: HashChangeEvent) => void' is not assignable to parameter of type '(this: Window, ev: Event) => any'.
      Types of parameters 'event' and 'ev' are incompatible.
        Type 'Event' is missing the following properties from type 'HashChangeEvent': newURL, oldURL
  Overload 2 of 2, '(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
    Argument of type '(this: Window, event: HashChangeEvent) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.
      Type '(this: Window, event: HashChangeEvent) => void' is not assignable to type 'EventListener'.
        Types of parameters 'event' and 'evt' are incompatible.
          Type 'Event' is not assignable to type 'HashChangeEvent'.

I tried many things and searched a lot in the net but found nothing.

Can someone please help me???

Thank you so much!!!
 
Last edited:
To me the messages seem to suggest that the listener function needs two arguments, one of type Window and one of type Event or HashChangeEvent.
I could be all wrong though, knowing beans about TypeScript or this particular feature. And these messages are terribly unhelpful... sheesh, you might as well go and program in C++ 🙄
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom