【发布时间】: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