Skip to content
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

Better attribution type #414

Closed
wants to merge 1 commit into from
Closed

Better attribution type #414

wants to merge 1 commit into from

Conversation

tunetheweb
Copy link
Member

Fixes #408

Copy link
Member

@brendankenny brendankenny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for the issue in #408, but the better solution would be some variation on

export type MetricWithAttribution =
  | CLSMetricWithAttribution
  | FCPMetricWithAttribution
  | FIDMetricWithAttribution
  | INPMetricWithAttribution
  | LCPMetricWithAttribution
  | TTFBMetricWithAttribution;

I think we were considering that with the Metric interface as well, but settled in doing that in a separate MetricType. I don't totally remember why, though. Documentation? Or to avoid a breaking change?

If it's a top-level union, then it both fixes the type widening in #408, but also lets you narrow without an assertion, e.g. if you have a generic metricWithAttribution you can check the name so the compiler knows which metric it's dealing with

if (metricWithAttribution.name === 'LCP') {
  const lcpEntry = metricWithAttribution.attribution.lcpEntry; // Property known to be present.
  // ...
}

nbd if this isn't a path we want to go down today, but both Metric and MetricWithAttribution feel like they're leaving typescript functionality behind and making them slightly harder to use than they need to be.

@tunetheweb
Copy link
Member Author

Alternatively we could introduce a new MetricWithAttributionType with that definition (which would be consistent with MetricType) and leave MetricWithAttribution alone for now. Then make the MetricWithAttribution change in v4, to avoid a breaking change for now incase we have any more v3 releases.

It does seem odd to have both interfaces and types defined though but maybe that's OK in this case?

@mcanu any thoughts here?

@tunetheweb
Copy link
Member Author

Anyone any more thoughts on this?

@brendankenny @mcanu @philipwalton

@philipwalton
Copy link
Member

@brendankenny can you take a look at this and make a recommendation? I think any change here might be a breaking change, so it'd be good to get this in the v4 release if possible.

@philipwalton
Copy link
Member

Also, once we have a decision we can update this PR's base branch to the v4 branch.

@brendankenny
Copy link
Member

If there's going to be a breaking change, definitely switching to the unions at the top level would be ideal (e.g. get rid of Metric and have MetricType be the real thing, similar with MetricWithAttribution), then move the types used for documentation to just be documentation.

I would love to give all the types a little refresh but I don't know if I'll have the time in the next ~24 hours if that's the goal for v4.

@tunetheweb
Copy link
Member Author

CLosing in favour of #471

@tunetheweb tunetheweb closed this May 3, 2024
@tunetheweb tunetheweb deleted the set-attribution-type branch May 3, 2024 11:05
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

Successfully merging this pull request may close these issues.

Type 'XXXMetricWithAttribution' is not assignable to type 'MetricWithAttribution'
3 participants