问题描述
对于这两种情况:
document.getElementById('body');
// or
window.document.getElementById('body');
我得到错误TS2304:找不到名称'窗口'。
我是否在 tsconfig.json
中遗漏了我应该安装的定义文件?
Am I missing something in tsconfig.json
for a definition file I should install?
我在运行 tsc
时收到消息,并在 vscode
I get the message when running tsc
and in vscode
tsconfig.json:
tsconfig.json:
{
"compilerOptions": {
"allowJs": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"jsx": "react",
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
"noImplicitAny": false,
"sourceMap": true,
"suppressImplicitAnyIndexErrors": true,
"target": "ES2016",
"typeRoots": [
"node_modules/@types/",
"typings/index.d.ts"
]
},
"exclude": [
"node_modules",
"**/*-aot.ts"
]
}
我的答案:
与 tsconfig.json一起使用
我定位 es5
并使用 lib :[es2015,dom]
My Answer:For use with tsconfig.json
I target es5
and use lib: ["es2015", "dom"]
推荐答案
看来问题是由定位 ES2016
。
您是出于某种原因定位的吗?如果您定位 es6
,错误可能会消失。
It seems that the problem is caused by targeting ES2016
.
Are you targeting that for a reason? If you target es6
the error will probably go away.
另一种选择是指定编译器的库使用:
Another option is to specify the libraries for the compiler to use:
tsc -t ES2016 --lib "ES2016","DOM" ./your_file.ts
这也应该使错误消失。
我'我不确定为什么默认情况下不使用lib,在它表示 - lib
选项:
I'm not sure why the libs aren't used by default, in the docs for compiler options it states for the --lib
option:
但它没有说明定位 ES2016 。
这可能是一个错误,尝试打开一个问题,如果你这样做请分享这里的链接。
But it doesn't state what are the default libraries when targeting
ES2016
.
It might be a bug, try to open an issue, if you do please share the link here.
这篇关于Typescript找不到名称窗口或文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!