【问题标题】:@types/gtag.js error: Argument of type '"config"' is not assignable to parameter of type '"consent"'@types/gtag.js 错误:'"config"' 类型的参数不可分配给'"consent"' 类型的参数
【发布时间】:2022-01-17 23:27:33
【问题描述】:

我正在尝试将 Google Analytics 添加到 Next.js 网站。我正在关注 JavaScript 中的 this 指南,但我的应用程序是 TypeScript。安装 @types/gtag.js 后,我收到此错误:

No overload matches this call.
  The last overload gave the following error.
    Argument of type '"config"' is not assignable to parameter of type '"consent"'.ts(2769)
index.d.ts(19, 5): The last overload is declared here.

在我的代码的这一行:

window.gtag('config', process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS, {
    page_path: url,
})

这里是类型定义:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/gtag.js/index.d.ts

我不确定类型是否不正确,但代码可能有效,因为它直接来自教程。

我的临时解决方法是声明gtag: any

declare global {
  interface Window {
    gtag: any
  }
}

【问题讨论】:

    标签: typescript google-analytics typescript-typings


    【解决方案1】:

    我收到此错误,我通过将类型 string 添加到 NEXT_PUBLIC_GOOGLE_ANALYTICS (在您的情况下)来解决此问题。就我而言,这是因为process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS 的类型是string | undefined

    【讨论】:

    • 这应该被标记为正确答案。谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 2021-09-10
    • 2019-06-24
    • 2021-06-14
    • 2021-02-03
    • 1970-01-01
    相关资源
    最近更新 更多