节点 ts 显示 "error TS2304: Cannot find name '__DEV__'"
node-ts shows "error TS2304: Cannot find name '__DEV__'"
我最近将 __DEV__
添加到我的 NodeJS 项目中的某个 TypeScript 文件中。在 VSCode 中,这未标记为错误。
但是,当我运行项目时,我立即得到错误
error TS2304: Cannot find name '__DEV__'.
我尝试将 /* global __DEV__ */
添加到文件的顶部。错误仍然存在。
我尝试在我 declare var __DEV__: boolean;
的位置添加一个 global.d.ts
文件。错误仍然存在。
这是我的 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"
]
}
]
}
在回购 https://github.com/TypeStrong/ts-node#help-my-types-are-missing.
上正式表达了关于打字的警告
综上所述,您可以通过执行以下操作来解决问题:
像这样为 typings
目录创建结构:
- tsconfig.json
- typings
-- global
--- index.d.ts
带index.d.ts
的是您的内容:
declare var __DEV__: boolean
然后将 typeRoots
添加到您的 tsconfig.json
:
{
"compilerOptions": {
"typeRoots" : ["./node_modules/@types", "./typings"]
}
}
最后,唯一真正起作用的是将 __DEV__
变量移动到 eval
:
const isInDebugMode = () => {
return eval('__DEV__');
}
不理想,但它完成了工作。
index.d.ts
中的声明只解决了设计时错误。运行错误不受其影响。
我最近将 __DEV__
添加到我的 NodeJS 项目中的某个 TypeScript 文件中。在 VSCode 中,这未标记为错误。
但是,当我运行项目时,我立即得到错误
error TS2304: Cannot find name '__DEV__'.
我尝试将 /* global __DEV__ */
添加到文件的顶部。错误仍然存在。
我尝试在我 declare var __DEV__: boolean;
的位置添加一个 global.d.ts
文件。错误仍然存在。
这是我的 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"
]
}
]
}
在回购 https://github.com/TypeStrong/ts-node#help-my-types-are-missing.
上正式表达了关于打字的警告综上所述,您可以通过执行以下操作来解决问题:
像这样为 typings
目录创建结构:
- tsconfig.json
- typings
-- global
--- index.d.ts
带index.d.ts
的是您的内容:
declare var __DEV__: boolean
然后将 typeRoots
添加到您的 tsconfig.json
:
{
"compilerOptions": {
"typeRoots" : ["./node_modules/@types", "./typings"]
}
}
最后,唯一真正起作用的是将 __DEV__
变量移动到 eval
:
const isInDebugMode = () => {
return eval('__DEV__');
}
不理想,但它完成了工作。
index.d.ts
中的声明只解决了设计时错误。运行错误不受其影响。