Skip to content

RTCDataChannel.send fails to compile #1973

Open
@achingbrain

Description

@achingbrain

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions