【发布时间】:2019-09-29 22:12:19
【问题描述】:
我已经安装了three@^0.103.0,它有自己的类型定义。
在我的项目的src/global.d.ts 中,我有:
import * as _THREE from 'three'
declare global {
const THREE: typeof _THREE
}
然后在src/global.ts我有
import * as THREE from 'three'
(window as any).THREE = { ...THREE }
然后在src/my-code.js 中,我尝试使用THREE 作为全局变量,f.e.
console.log(new THREE.Vector3(1,2,3)) // ERROR, 'THREE' refers to a UMD global, but the current file is a module. Consider adding an import instead.
它告诉我'THREE' refers to a UMD global, but the current file is a module. Consider adding an import instead.。
当我跳转到THREE 的定义时,它会将我带到node_modules/three/src/Three.d.ts,这不是我的src/global.d.ts 文件。
那么,TypeScript 似乎忽略了我的 global.d.ts 定义?
我的tsconfig.json 包含
"allowJs" true,
"checkJs" true,
"include": ["src/**/*"]
我有global.d.ts 在src 内。
如果我添加
/// <reference path="./global.d.ts" />
到 src/my-code.js 文件 (JavaScript) 的顶部,然后它可以工作,我可以跳转到 THREE 的定义,这会将我带到我的 global.d.ts 文件。
为什么没有reference 评论就不能工作?
【问题讨论】:
-
那么,您要覆盖全局
THREE?这样做的理由是什么? -
@AndrewEisenberg 没有全局三;我想将它导入我的
global.ts模块并使其成为全球性的。但是在我的src/my-code.ts文件中,我收到了错误'THREE' refers to a UMD global, but the current file is a module. Consider adding an import instead.。如何导入 THREE 并使其在一个模块中成为全局变量,并在其他模块中用作全局变量? -
Mohamed Hegazy 说“一个模块(即一个至少有一个顶级导入或导出的文件)有自己的范围,除非导出,否则此文件中的声明在其外部不可见。”在github.com/Microsoft/TypeScript/issues/…。这是问题吗?我在一个模块中导入三个并声明一个全局,并且这个定义在文件之外不可见(除非我使用
reference例如)?如何从'three'获取定义并使其成为全球性的? -
@AndrewEisenberg 我认为我的设置有问题:即使我在我的
global.d.ts文件中只放了declare var foo: number,然后在任何其他.js文件中我尝试console.log(foo)和它抱怨找不到foo。我想我可能会结束这个问题。 -
@AndrewEisenberg 哦,我明白了问题所在。我有
src/global.ts和src/global.d.ts,TypeScript 显然合并为一个(或其他东西)。一旦我以不同的方式重命名它们(例如make-global.ts和global.d.ts),一切就开始工作了。我正在拔头发。
标签: javascript typescript three.js type-declaration