按照惯例,开发人员会在全球范围内包含全局 dom 类型并在全球范围内使用它们。
{
"compilerOptions": {
"lib": [
"dom"
]
},
}
Run Code Online (Sandbox Code Playgroud)
是否可以显式使用 dom 类型?
就像是:
import { MessagePort, HTMLElement } from 'dom'
Run Code Online (Sandbox Code Playgroud) 我在尝试编译没有 DOM 类型的 TypeScript Node 项目时遇到了一个问题,但 TypeScript 一直包含 DOM 类型。这是我的tsconfig.json
:
{
"compilerOptions": {
"lib": [
"ES6"
],
"target": "es6",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在使用"lib": ["ES6"]
which 是为了摆脱 DOM 类型。使用tsc --showconfig
我可以验证这是我正在使用的配置。但是,VSCode 的 IntelliSense 允许我写出 HTML 元素的类型,并且在编译时,我得到一个错误,特别是在谈论与 中的类型冲突时lib.dom.d.ts
,即使tsconfig
假定排除它们(省略号是我自己的):
node_modules/@types/webgl2/index.d.ts:582:13 - error TS2403: Subsequent variable declarations must have the same type. Variable 'WebGL2RenderingContext' must be of type (...), but here has type (...).
582 …
Run Code Online (Sandbox Code Playgroud)