Skip to content

RTCDataChannel.send fails to compile #1973

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

Open
achingbrain opened this issue Apr 11, 2025 · 0 comments
Open

RTCDataChannel.send fails to compile #1973

achingbrain opened this issue Apr 11, 2025 · 0 comments

Comments

@achingbrain
Copy link

Code that should compile but doesn't

function sendIt (peerConnection: RTCPeerConnection, data: ArrayBufferView | string): void {
  const dc = peerConnection.createDataChannel('')
  dc.send(data)
}

Reason

RTCDataChannel.send accepts string | Blob | ArrayBuffer | ArrayBufferView but because the .send method is declared as multiple overrides you have to work out the type of the argument before you invoke the method, even though you don't actually do anything to the data before sending:

// lib.dom.d.ts line 18,000
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/RTCDataChannel/send) */
send(data: string): void;
send(data: Blob): void;
send(data: ArrayBuffer): void;
send(data: ArrayBufferView): void;
function sendIt (peerConnection: RTCPeerConnection, data: ArrayBufferView | string): void {
  const dc = peerConnection.createDataChannel('')

  if (typeof data === 'string') {
    dc.send(data)
  } else {
    dc.send(data)
  }
}

Changing it to the following works:

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/RTCDataChannel/send) */
send(data: string | Blob | ArrayBuffer | ArrayBufferView) void;

Reference

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

No branches or pull requests

1 participant