Open stefanprobst opened 9 months ago
Hey @stefanprobst,
You are right, the types are not included in the final build. I found the same issue 4 years ago. #13512 The "workaround" was to define the custom metric interface NextWebVitalsMetric
.
For example, you can do this:
import type { NextWebVitalsMetric } from "next/app";
import { useReportWebVitals } from "next/web-vitals";
export function Analytics() {
useReportWebVitals((metric: NextWebVitalsMetric) => {
console.log(metric.name);
});
return null;
}
@waigel thanks for the hint! i wasn't aware of NextWebVitalsMetric
.
maybe this is just a docs issue then? i'm not seeing it mentioned on https://nextjs.org/docs/app/building-your-application/optimizing/analytics#web-vitals?
To me, the custom interface looks more like a workaround than a real bug fix. Perhaps the problem should be solved properly in the long term instead of forcing every user to use a different interface? From the PR or the issue 4 years ago, it's not clear to me why they decided to go down this route...
The custom interface NextWebVitalsMetric
is mentioned here: https://nextjs.org/docs/app/api-reference/next-config-js/webVitalsAttribution
Link to the code that reproduces this issue
https://github.com/stefanprobst/issue-next-webvitals
To Reproduce
components/analytics.tsx
metric.name
is typed asany
(you can also ctrl+click onuseReportWebVitals
to see that the type importimport type { Metric } from 'next/dist/compiled/web-vitals';
does not resolve)Current vs. Expected behavior
the
useReportWebVitals
hook accepts a callback(metric: Metric) => void
, where the typeMetric
is imported fromnext/dist/compiled/web-vitals
here. however,next/dist/compiled/web-vitals
does not include a.d.ts
file, only the.js
, so the type import does not resolve.Verify canary release
Provide environment information
Which area(s) are affected? (Select all that apply)
TypeScript (plugin, built-in types)
Additional context
No response