TypeScript 项目中缺少基本的 DOM 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42603783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing basic DOM types in TypeScript project
提问by James McMahon
I am setting a web app up in TypeScript and I seem to be missing some basic types I need.
我正在用 TypeScript 设置一个 Web 应用程序,但似乎缺少一些我需要的基本类型。
When I compile (npm run build
), I get the following errors,
当我编译 ( npm run build
) 时,出现以下错误,
error TS2304: Cannot find name 'HTMLElement'.
error TS2304: Cannot find name 'SVGElement'.
error TS2304: Cannot find name 'EventTarget'.
error TS2304: Cannot find name 'TouchEvent'.
error TS2304: Cannot find name 'MouseEvent'.
error TS2304: Cannot find name 'PointerEvent'.
错误 TS2304:找不到名称“HTMLElement”。
错误 TS2304:找不到名称“SVGElement”。
错误 TS2304:找不到名称“EventTarget”。
错误 TS2304:找不到名称“TouchEvent”。
错误 TS2304:找不到名称“MouseEvent”。
错误 TS2304:找不到名称“PointerEvent”。
Based on my Googling I assuming I am missing something basic in my project setup. It seems like these types are just assumed to be there with Typescript.
基于我的谷歌搜索,我假设我的项目设置中缺少一些基本的东西。似乎这些类型只是假设存在于 Typescript 中。
EDIT: Specially it should be part of the ES6 types, https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es6.d.ts.
编辑:特别是它应该是 ES6 类型的一部分,https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es6.d.ts。
Here is my package.json
file:
这是我的package.json
文件:
{
"name": "wip",
"version": "1.0.0",
"description": "",
"main": "index.html",
"dependencies": {
"hammerjs": "2.0.8"
},
"devDependencies": {
"@types/chai": "3.4.35",
"@types/mocha": "2.2.39",
"@types/node": "7.0.5",
"@types/hammerjs": "2.0.34",
"chai": "3.5.0",
"mocha": "3.2.0",
"safe-mock": "0.2.0",
"ts-node": "2.1.0",
"tslint": "4.5.1",
"typescript": "2.2.1",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.4.1"
},
"scripts": {
"test": "mocha test --require ts-node/register test/**/*.ts && npm run build",
"dev": "webpack-dev-server --watch --content-base . -d --progress",
"build": "tsc"
},
"author": "",
"license": "ISC"
}
Any suggestions?
有什么建议?
回答by Shaun Luttin
Try adding the following lib
section to your tsconfig.json
file.
尝试将以下lib
部分添加到您的tsconfig.json
文件中。
{
"compilerOptions": {
"lib": [
"es2016",
"dom"
]
}
}
回答by Xeoncross
Additional answer for testing.
测试的附加答案。
If using mocha, you also need to tell mocha about the DOM environment using jsdom
.
如果使用 mocha,还需要使用jsdom
.
$ npm install jsdom jsdom-global --save-dev
So your "test" script would add -r jsdom-global/register
:
所以你的“测试”脚本会添加-r jsdom-global/register
:
{
"scripts": {
"test": "mocha test -r ts-node/register -r jsdom-global/register test/**/*.ts && npm run build"
}
}