@types/gtag.js error: Argument of type '"config"' is not assignable to parameter of type '"consent"'

Viewed 1266

I'm trying to add Google Analytics to a Next.js site. I'm following this guide which is in JavaScript but my app is TypeScript. After I install @types/gtag.js I am getting this error:

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.

on this line of my code:

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

Here are the type definitions: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/gtag.js/index.d.ts

I'm not sure if the types are incorrect but the code presumably works since it's straight from the tutorial.

My temporary workaround is to declare gtag: any:

declare global {
  interface Window {
    gtag: any
  }
}
1 Answers

I got this error and I fixed this by add type string to NEXT_PUBLIC_GOOGLE_ANALYTICS (in your case). In my case it because type of process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS is string | undefined.

Related