使用 forge-viewer/node_modules/@types/three 中的三个会导致编译器错误

Using THREE from forge-viewer/node_modules/@types/three causes compiler error

我需要使用需要 THREE.Vector4 类型参数的 Viewer.setThemingColor() 方法。

我正在使用 Angular 9,我已经安装了 @types/forge-viewer,其中还包括在 forge-viewer/node_modules/@types/three.

处的三个定义

Visual Studio 代码获取 THREE.Vector4 的定义并插入 import * as THREE from 'forge-viewer/node_modules/@types/three'ng build 命令抛出错误:

Can't resolve 'forge-viewer/node_modules/@types/three'.

我尝试在 tsconfig.app.json(类型)和 tsconfig.json(typeRoots)中添加三个,但没有帮助。

额外安装 @types/three 并从 'three' 导入将修复编译器错误,但它会在不同位置创建重复定义,并且 Visual Studio 代码只会自动看到来自 forge-viewer forge-viewer/node_modules/@types/three 而不是 three.

她有什么问题,如何解决?

我找到了一种方法,可以在代码中引用 THREE 对象而不安装 @types/three 时避免编译器错误。在引用了 THREE 形式的 TS 文件中,例如 THREE.Vector4,而不是从模块中导入 THREE 插入“假”声明

declare var THREE: any;

积极的结果是:

  1. 没有编译错误;
  2. 没有从 webpack 加载 three.js 的第二个副本

负面结果是在 VS Code 中丢失 TypeScript 智能感知,但可以通过临时安装 @types/three、开发和测试代码然后删除 @types/three.

来缓解这种情况