Typescript找不到名称窗口或文档 [英] Typescript cannot find name window or document

查看:1703
本文介绍了Typescript找不到名称窗口或文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这两种情况:

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:


注意:如果未指定--lib,则注入默认库。注入的
默认库是:
$ b $b►对于--target ES5:DOM,ES5,ScriptHost
$ b $b►对于--target ES6:DOM,ES6,DOM.Iterable ,ScriptHost

Note: If --lib is not specified a default library is injected. The default library injected is:
► For --target ES5: DOM,ES5,ScriptHost
► For --target ES6: DOM,ES6,DOM.Iterable,ScriptHost

但它没有说明定位 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找不到名称窗口或文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆