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

时间:2020-09-07 06:57:20

标签: node.js typescript ts-node

我最近将__DEV__添加到了NodeJS项目中的某些TypeScript文件中。在VSCode中,这没有标记为错误。

但是,当我运行项目时,我立即收到错误消息

error TS2304: Cannot find name '__DEV__'.

我尝试将/* global __DEV__ */添加到文件顶部。错误仍然存​​在。

我尝试在global.d.ts中添加一个declare var __DEV__: boolean;文件。错误仍然存​​在。

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

编辑:该项目通过VSCode中的launch.json文件启动。这是它的内容:

{
    "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"
            ]
        }
    ]
}

2 个答案:

答案 0 :(得分:2)

在回购https://github.com/TypeStrong/ts-node#help-my-types-are-missing上正式表达了有关打字的警告。

总结一下,您可以通过以下方法解决问题:

像这样为{ textPayload: "ERROR: logging before flag.Parse: E0907 16:33:58.813216 1 nanny_lib.go:128] Get https://10.0.0.1:443/api/v1/nodes?resourceVersion=0: http2: no cached connection was available" insertId: "zzz" resource: { type: "k8s_container" labels: { project_id: "zzz" namespace_name: "kube-system" container_name: "metrics-server-nanny" pod_name: "metrics-server-v0.3.6-7b7d6c7576-jksst" cluster_name: "zzz" location: "zzz" } } timestamp: "2020-09-07T16:33:58.813411604Z" severity: "ERROR" labels: { gke.googleapis.com/log_type: "system" k8s-pod/version: "v0.3.6" k8s-pod/k8s-app: "metrics-server" k8s-pod/pod-template-hash: "7b7d6c7576" } logName: "projects/zzz/logs/stderr" receiveTimestamp: "2020-09-07T16:34:05.273766386Z" } 目录创建结构:

typings

- tsconfig.json - typings -- global --- index.d.ts 是您的内容

index.d.ts

然后将declare var __DEV__: boolean 添加到您的typeRoots

tsconfig.json

答案 1 :(得分:0)

最后,唯一有效的方法是将int y = 42; x = &y; 变量移至__DEV__

eval

不太理想,但是确实可以。

const isInDebugMode = () => { return eval('__DEV__'); } 中的声明仅解决了设计时错误。运行时错误不受它的影响。