简体   繁体   English

node-ts 显示“错误 TS2304:找不到名称 '__DEV__'”

[英]node-ts shows “error TS2304: Cannot find name '__DEV__'”

I recently added __DEV__ to some TypeScript file in my NodeJS project.我最近将__DEV__添加到我的__DEV__中的某个 TypeScript 文件中。 In VSCode, this is not marked as an error.在 VSCode 中,这不会被标记为错误。

However, when I run the project, I immediately get the error但是,当我运行该项目时,我立即收到错误

error TS2304: Cannot find name '__DEV__'.

I tried adding /* global __DEV__ */ to the top of the file.我尝试将/* global __DEV__ */到文件顶部。 Error still there.错误仍然存​​在。

I tried adding a global.d.ts file where I declare var __DEV__: boolean;我尝试在我declare var __DEV__: boolean;地方添加一个global.d.ts文件declare var __DEV__: boolean; . . Error still there.错误仍然存​​在。

Here's my tsconfig:这是我的 tsconfig:

{
 "compilerOptions": {
  "target": "es6",
  "lib": [
   "es2017","es2015","dom","es6"
  ],
  "module": "commonjs",
  "outDir": "./",
  "sourceMap": true,
  "esModuleInterop": true,
  "strict": false,
  "resolveJsonModule": true,
  "downlevelIteration": true
 },
 "include": [
  "**.ts"
 ],
 "exclude": [
  "node_modules"
 ]
}

EDIT: The project is launched via a launch.json file in VSCode.编辑:该项目是通过launch.json中的launch.json文件启动的。 Here's its contents:这是它的内容:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Current TS File",
            "type": "node",
            "request": "launch",
            "args": ["${relativeFile}"],
            "runtimeArgs": ["--nolazy", "-r", "ts-node/register", "--max-old-space-size=32768"],
            "cwd": "${workspaceRoot}",
            "protocol": "inspector",
            "internalConsoleOptions": "openOnSessionStart",
            "console": "integratedTerminal",
            "stopOnEntry": false,
            "skipFiles": [
                "${workspaceFolder}/node_modules/**/*.js",
                "<node_internals/**/*.js"
            ]
        }
    ]
}

There is a caveat regarding to typing which is officially expressed on the repohttps://github.com/TypeStrong/ts-node#help-my-types-are-missing .有一个关于打字的警告,在 repohttps://github.com/TypeStrong/ts-node#help-my-types-are-missing上正式表达。

To sump up, you can resolve the problem by doing things as following:综上所述,您可以通过以下操作来解决问题:

Create the structure for typings dir like this:像这样为typings目录创建结构:

- tsconfig.json
- typings
-- global
--- index.d.ts

with the index.d.ts is your content: index.d.ts是你的内容:

declare var __DEV__: boolean

Then add typeRoots to your tsconfig.json :然后将typeRoots添加到您的tsconfig.json

{
  "compilerOptions": {
    "typeRoots" : ["./node_modules/@types", "./typings"]
  }
}

In the end, the only thing that really worked was moving the __DEV__ variable to an eval :最后,唯一真正有效的是将__DEV__变量移动到eval

const isInDebugMode = () => {
  return eval('__DEV__');
}

Not ideal, but it did the job.不理想,但它完成了工作。

The declaration in index.d.ts does only resolve the design-time error. index.d.ts中的声明仅解决设计时错误。 The runtime error is not affected by it.运行时错误不受它的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM