TypeScript-DOM-lib-generator
TypeScript-DOM-lib-generator copied to clipboard
RTCDataChannel.send fails to compile
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
- https://www.w3.org/TR/webrtc/#webidl-rtcdatachannel
- https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/send