我在尝试编译没有 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 declare var WebGL2RenderingContext: {
~~~~~~~~~~~~~~~~~~~~~~
C:/Users/username/AppData/Roaming/npm/node_modules/typescript/lib/lib.dom.d.ts:16316:13
16316 declare var WebGL2RenderingContext: {
~~~~~~~~~~~~~~~~~~~~~~
'WebGL2RenderingContext' was also declared here.
Run Code Online (Sandbox Code Playgroud)
怎样才能的两个定义WebGL2RenderingContext冲突,如果我不包括DOM类型的我tsconfig?
blu*_*e10 12
我们遇到了同样的问题。虽然在您的情况下,这可能是另一个罪魁祸首,但您可以通过以下方法缩小范围:
创建一个空项目,获取并使用一个最小的代码片段,该代码片段仅在以下范围内tsconfig.json才有效:lib.dom.d.ts
// This works in a browser, but should fail to compile for nodejs, because in
// nodejs, and explicit import is needed.
const time = performance.now();
Run Code Online (Sandbox Code Playgroud)
如果编译失败,你就知道你的tsconfig.json总体情况是好的。
现在慢慢引入第三部分依赖项,即启动 npm - 将“损坏”项目中的依赖项一一安装,直到编译突然通过。
在我们的例子中,有问题的依赖性是@types/supertest,它反过来又被拉进来了@types/superagent。问题@types/superagent是它包含三斜杠指令:
/// <reference lib="dom" />
Run Code Online (Sandbox Code Playgroud)
lib.dom.d.ts这似乎具有包含在编译中的效果,有效地覆盖"lib": ["ES6"]了您在tsconfig.json.
也许您还可以通过在依赖项中专门查找该行来更快地找到罪魁祸首。
尽管如何处理这个问题,我仍然没有一个好的答案。显然,安装依赖项不应导致编译以静默方式传递在非浏览器上下文中应为编译错误的情况。我在 TypeScript 存储库上打开了一个问题,看看是否可以解决这个问题,并且还有另一个针对@types/superagent.
bas*_*rat -1
使用不带 DOM 类型的 TypeScript(对于节点)
您的 tsconfig 是执行此操作的正确方法。IE
"lib": [
"ES6"
],
Run Code Online (Sandbox Code Playgroud)
您的工作区中很可能有另一个tsconfig.json 正在被 vscode 拾取。
| 归档时间: |
|
| 查看次数: |
936 次 |
| 最近记录: |