javascript - typescript 找不到名称窗口或文件

标签 javascript typescript typescript2.0

无论哪种情况:

document.getElementById('body');
// or
window.document.getElementById('body');

我收到错误 TS2304:找不到名称“window”。

我是否在 tsconfig.json 中遗漏了一些我应该安装的定义文件?

我在运行 tscvscode 时收到消息

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

最佳答案

使用

"lib": ["dom"]

在 tsconfig.json 中

例如

{
  "compilerOptions": {
    "lib": ["es5", "es6", "dom"],
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es6",
    "moduleResolution": "node",
    "jsx": "react"
  },
  "include": ["./src/**/*"]
}

关于javascript - typescript 找不到名称窗口或文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41336301/

相关文章:

javascript - 隐藏浏览器的地址栏(弹出)

javascript - 为什么要在 propTypes 中使用 static

typescript - 使用类表达式时是否支持循环引用?

visual-studio-2015 - 使用 Typescript 2.0 的 MomentJS 类型定义

typescript - 如何在 TypeScript 2 中扩充 UMD 模块定义

javascript - 在 jquery 中点击后退按钮后,删除的内容被替换回来

javascript - jQuery:是否可以获取 css 值数组?

typescript - 汇总生成 typescript 错误 'Unexpected token'

angular - 类型 'BehaviorSubject<false>' 不可分配给类型 'BehaviorSubject<boolean>'

typescript - 在 whatwg-fetch 类型声明文件中找不到名称 'Symbol'