You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript版Create React App中reportWebVitals的预期返回类型咨询

解决reportWebVitals的ESLint类型缺失报错

reportWebVitals函数的预期返回类型是void——它本身没有返回值,仅用于接收并处理Web Vitals的性能数据。

直接给函数加上void返回类型就能解决这个ESLint错误,修正后的代码如下:

const reportWebVitals = (onPerfEntry?: ReportHandler): void => {
  if (onPerfEntry && onPerfEntry instanceof Function) {
    import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
      getCLS(onPerfEntry);
      getFID(onPerfEntry);
      getFCP(onPerfEntry);
      getLCP(onPerfEntry);
      getTTFB(onPerfEntry);
    });
  }
};

补充说明:

  • ReportHandler是web-vitals包自带的类型,create-react-app的TypeScript模板已经自动引入了相关类型定义,直接使用即可。如果编辑器提示找不到该类型,确认项目里的@types/web-vitals依赖是否存在(create-react-app会默认安装这个依赖)。
  • 要是觉得手动给每个边界函数加类型太繁琐,也可以在ESLint配置里把@typescript-eslint/explicit-module-boundary-types规则改为警告或者关闭,但从TypeScript类型规范的角度来说,明确返回类型是更推荐的做法。

内容的提问来源于stack exchange,提问作者Fiddle Freak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 23:50:25