gpt4 book ai didi

javascript - typeof window == "undefined"在使用 ts-node 时抛出错误

转载 作者:行者123 更新时间:2023-12-01 21:36:28 24 4
gpt4 key购买 nike

我有一些代码,其中我正在使用 typeof window == "undefined" 检查是否有浏览器环境。当我使用 ts-node 启动这段代码时,我得到了这个:

typings/Console.ts:36:10 - error TS2304: Cannot find name 'window'.

36 typeof window == "undefined"
~~~~~~

AFAIK typeof 是一种可以安全使用 undefined variable 的运算符,它在浏览器和 NodeJS 环境中都运行良好。但就我开始将它与 ts-node 一起使用而言,它开始抛出错误。

我的tsconfig.json

{
"compilerOptions": {
"module": "CommonJS",
"target": "es5",
"moduleResolution": "node",
"baseUrl": "src",
"allowSyntheticDefaultImports": true,
"noImplicitAny": true,
"strict": false,
"sourceMap": true,
"traceResolution": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strictNullChecks": true,

"allowJs": false,
"declaration": false,
"removeComments": true,
"noLib": false,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true,
"types": [ "node" ],
"lib": [ "es6" ],
"downlevelIteration": true,
"resolveJsonModule": true,
"typeRoots": [
"../node_modules/@types"
]
}
}

那么,有什么诀窍呢?提前致谢!

最佳答案

尝试在 tsconfig "DOM"中添加到 lib

关于javascript - typeof window == "undefined"在使用 ts-node 时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61871197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com