microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.94k stars 12.47k forks source link

RTCStatsReport should inherit or extend from Map #58995

Open ibc opened 4 months ago

ibc commented 4 months ago

⚙ Compilation target

ESNEXT

⚙ Library

No idea why this item is mandatory

Missing / Incorrect Definition

RTCStatsReport is defined as follows:

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/RTCStatsReport) */
interface RTCStatsReport {
    forEach(callbackfn: (value: any, key: string, parent: RTCStatsReport) => void, thisArg?: any): void;
}

As per documentation, RTCStatsReport is a Map-like object so it should include methods such as values(), entries() and members such as size. Note that it must NOT include delete(), clear() or set() since it's read-only (see doc links at the bottom).

However RTCStatsReport TypeScript definition only exposes forEach().

Sample Code

const pc = new RTCPeerConnection();

pc.createDataChannel('foo', { negotiated: true, id: 123, ordered: true });

const stats = await pc.getStats();

console.log('--- num stats:', stats.size);

for (const [id, stat] of stats) {
    console.log('--- stat:', stat);
    console.log('--- stats.get(id):', stats.get(id));
}

Produces:

--- num stats: 2
--- stat: {id: 'D37', timestamp: 1719306842612.802, type: 'data-channel', bytesReceived: 0, bytesSent: 0, …}
--- stats.get(id): {id: 'D37', timestamp: 1719306842612.802, type: 'data-channel', bytesReceived: 0, bytesSent: 0, …}
--- stat: {id: 'P', timestamp: 1719306842612.802, type: 'peer-connection', dataChannelsClosed: 0, dataChannelsOpened: 0}
--- stats.get(id): {id: 'P', timestamp: 1719306842612.802, type: 'peer-connection', dataChannelsClosed: 0, dataChannelsOpened: 0}

Documentation Link

RyanCavanaugh commented 4 months ago

Sample Code: N/A

The reason we ask for sample code is so that we can inspect a value of this type at runtime and verify the things you're saying about it.

ibc commented 4 months ago

The reason we ask for sample code is so that we can inspect a value of this type at runtime and verify the things you're saying about it.

Thanks. I've added sample code.