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

Event type of midiInput.onmidimessage wrong #1571

Closed
h-a-n-n-e-s opened this issue Jun 14, 2023 · 7 comments · Fixed by #1652
Closed

Event type of midiInput.onmidimessage wrong #1571

h-a-n-n-e-s opened this issue Jun 14, 2023 · 7 comments · Fixed by #1652

Comments

@h-a-n-n-e-s
Copy link

This

midiInput.onmidimessage = handleMIDIMessage;
with
handleMIDIMessage = (event:MIDIMessageEvent) => {...}

runs perfectly fine in the browser, but the TypeScript compiler throws this error:

Type '(event: MIDIMessageEvent) => void' is not assignable to type '(this: MIDIInput, ev: Event) => any'.
Types of parameters 'event' and 'ev' are incompatible.
Property 'data' is missing in type 'Event' but required in type 'MIDIMessageEvent'.

Shouldn't midiInput.onmidimessage have the type (this: MIDIInput, ev: MIDIMessageEvent) => any instead of (this: MIDIInput, ev: Event) => any ?

According to this
https://developer.mozilla.org/en-US/docs/Web/API/MIDIMessageEvent
it should.

Thanks!

@christiaanwesterbeek
Copy link

I encountered this too. In my Chrome browser I can do this and it works.

midiInput.onmidimessage = (msg) => {
    const [status, note, velocity] = msg.data;
    console.log(msg);
}

But still typescript complains that:

Property 'data' does not exist on type 'Event'. ts(2339)

I changed my code to the following and the typescript error is gone.

midiInput.onmidimessage = (msg: MIDIMessageEvent) => {
    const [status, note, velocity] = msg.data;
    console.log(msg);
}

@h-a-n-n-e-s
Copy link
Author

@christiaanwesterbeek
Yes, this is exactly what I do too, but Typescript erroneously expects type Event for msg and throws the error.

@fa-sharp
Copy link
Contributor

Just submitted a PR to fix this :)

@h-a-n-n-e-s
Copy link
Author

@fa-sharp
Why can I not see your merge in my typescript 5.3.3 installation?
Shouldn't all updates here automatically be included in all typescript releases?

@fa-sharp
Copy link
Contributor

@h-a-n-n-e-s Hmm, good question! I did assume it was automatic but maybe it isn't anymore.

I did a little digging, and I think the last time the DOM types from this library were merged into TypeScript was this PR: microsoft/TypeScript#55798. So we might need to wait until TS 5.4 until the new types are merged? 🤷

@h-a-n-n-e-s
Copy link
Author

@fa-sharp Oh, I see. Then lets wait for the next minor release! ☺️ Thanks!

@h-a-n-n-e-s
Copy link
Author

@fa-sharp In TS 5.4 it's merged! ☺️ Thanks again for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants