Typescript 未检测包@types/gtag.js 声明的全局"gtag" 变量

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

Typescript 无法检测到我的全局 gtag var.

我安装了 @types/gtag.js。这是它的 index.d.ts 文件:

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

这是我遇到的错误:

所以我把这个添加到我的 .eslintrc.js

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

现在我只收到这个错误:

Cannot find name 'gtag'.

这是我的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"
  ]
}

如何让 Typescript 检测声明的 gtag 全局变量?

我的问题与 tsconfig.json

上的 types 属性 有关

tsconfig.json

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

删除这些行后,系统检测到全局 gtag 类型。

来自https://www.typescriptlang.org/tsconfig#types