Search code examples
typescripttsconfiggtag.js.d.ts

Typescript not detecting global "gtag" variable declared by the package @types/gtag.js


Typescript is not being able to detect my global gtag var.

I have the @types/gtag.js installed. Here is its index.d.ts file:

node_modules/@types/gtag.js/index.d.ts

enter image description here

This is the error I was getting:

enter image description here

So I added this to my .eslintrc.js

globals: {
  gtag: typeof gtag   // IT DOES DETECT THE gtag TYPE HERE
},

Now I'm getting only this error:

enter image description here

Cannot find name 'gtag'.

Here is my tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "strictNullChecks": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "types": [
      "node"
    ],
    "baseUrl": ".",
    "paths": {
      "@app/*": [
        "./app/*"
      ],
      "@lib/*": [
        "./lib/*"
      ]
    },
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

What can I do to make Typescript detect the declared gtag global variable?


Solution

  • My problem was related to the types property on the tsconfig.json

    tsconfig.json

    //  "types": [
    //    "node"
    //  ],
    

    Once I've removed those lines, the global gtag type was detected.

    From https://www.typescriptlang.org/tsconfig#types

    enter image description here