【问题标题】:Typescript jest global variable exampleTypescript jest 全局变量示例
【发布时间】:2021-08-24 22:45:30
【问题描述】:

这是同一个问题,在这里正确回答 jest global variable example ,但问题是 Typescript。

console.log(__DEV__);

结果:

error TS2304: Cannot find name __DEV__.

全局变量 __DEV__ 在 package.json 中定义为:

  "jest": {
    "globals": {
      "__DEV__": true
    }
  }

有没有人解决或解决?

相关笑话文档:https://jestjs.io/docs/en/next/configuration#globals-object

【问题讨论】:

    标签: typescript jestjs


    【解决方案1】:

    在 ts-jest 中,需要用 "ts-jest" 包装一些配置值:

      "jest": {
        "globals": {
          "ts-jest": {
            "__DEV__": true
          }
        }
      }
    

    并且可以通过以下代码访问:

    console.log((global as any).__DEV__
    

    在这里找到答案https://kulshekhar.github.io/ts-jest/docs/

    【讨论】:

      【解决方案2】:

      一个合适的解决办法是写一个.d.ts,引入全局变量declare,让tsconfig.json包含这个.d.ts文件。

      【讨论】:

        猜你喜欢
        • 2017-06-02
        • 2017-03-19
        • 2018-07-12
        • 1970-01-01
        • 2016-05-28
        • 1970-01-01
        • 2013-06-12
        • 2019-02-24
        • 1970-01-01
        相关资源
        最近更新 更多