【问题标题】:Typescript not detecting global "gtag" variable declared by the package @types/gtag.jsTypescript 未检测到包 @types/gtag.js 声明的全局“gtag”变量
【发布时间】:2021-07-20 12:19:45
【问题描述】:

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
},

现在我只收到这个错误:

找不到名称“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 全局变量?

【问题讨论】:

    标签: typescript tsconfig gtag.js .d.ts


    【解决方案1】:

    我的问题与tsconfig.json 上的types 属性有关

    tsconfig.json

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

    一旦我删除了这些行,就会检测到全局 gtag 类型。

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

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 2022-09-23
      • 1970-01-01
      • 2017-07-25
      • 2020-02-10
      • 2013-08-12
      • 1970-01-01
      • 2013-07-03
      • 2015-07-25
      相关资源
      最近更新 更多