GoogleChrome / web-vitals

Essential metrics for a healthy site.
https://web.dev/vitals
Apache License 2.0
7.59k stars 415 forks source link

Make `bindReporter` generic over metric type #359

Closed brendankenny closed 1 year ago

brendankenny commented 1 year ago

Can land after #356

Parameterizes the bindReporter type on the metric passed in so that the callback can now be typed specifically for that metric (e.g. a result for CLSMetric must be using a CLSReportCallback).

This required initMetric to be parameterized similarly, so the returned metric type is now based on the metric name passed in.

let metric = initMetric('FCP');
//  ^ FCPMetric

The main change required was using a union of the metric types wherever the base Metric interface is currently used. This leaves most uses of it unchanged, but allows narrowing to which specific metric is being used based on the name property.

I renamed the existing Metric to be MetricBase and named the union Metric to keep the uses of it unchanged, but if someone has better naming suggestions, changing is no problem :)