简体   繁体   English

如何覆盖TypeScript UMD全局类型定义?

[英]How to override a TypeScript UMD global type definition?

I have installed three@^0.103.0 , which has its own type definitions. 我已经安装了three@^0.103.0 ,它具有自己的类型定义。

In src/global.d.ts of my project I have: 在我的项目的src/global.d.ts ,我有:

import * as _THREE from 'three'

declare global {
    const THREE: typeof _THREE
}

Then in src/global.ts I have 然后在src/global.ts

import * as THREE from 'three'
(window as any).THREE = { ...THREE }

Then in src/my-code.js I am trying to use the THREE as a global variable, fe 然后,在src/my-code.js我想用THREE作为一个全局变量,FE

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.

It tells me that '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' refers to a UMD global, but the current file is a module. Consider adding an import instead. .

When I jump to the definition of THREE , it takes me to node_modules/three/src/Three.d.ts , which is not my src/global.d.ts file. 当我跳到THREE的定义时,它将带我到node_modules/three/src/Three.d.ts ,这不是我的src/global.d.ts文件。

So, it seems like TypeScript is ignoring my global.d.ts definitions? 因此,似乎TypeScript忽略了我的global.d.ts定义?

My tsconfig.json contains 我的tsconfig.json包含

  "allowJs" true,
  "checkJs" true,
  "include": ["src/**/*"]

and I have global.d.ts is inside of src . 而且我有global.d.tssrc内部。

If I add 如果我加

/// <reference path="./global.d.ts" />

to the top of src/my-code.js file (JavaScript), then it works, and I can jump to the definition of THREE which takes me to my global.d.ts file. 然后将其src/my-code.js文件(JavaScript)的顶部,然后我就可以跳转到THREE定义,这使我进入了global.d.ts文件。

Why does it not work without the reference comment? 如果没有reference注释,为什么它不起作用?

The above should work. 以上应该可以。 There's only one small gotcha: 只有一个小陷阱:

The two global files have the same name! 这两个global文件具有相同的名称! (ie global.ts and global.d.ts ) (即global.tsglobal.d.ts

In this case, TypeScript seems to merge them together (or something), and thus seems to treat global as the same module (as in import './global' being ambiguous). 在这种情况下,TypeScript似乎将它们合并在一起(或某种形式),因此似乎将global视为相同的模块(如import './global'中的模棱两可)。

So, by renaming one module to a different name, it all works. 因此,通过将一个模块重命名为一个不同的名称,所有这些都可以工作。

For example, rename src/global.ts to src/make-global.ts , and leave src/global.d.ts , and it will work. 例如,将src/global.ts重命名为src/make-global.ts ,并保留src/global.d.ts ,它将起作用。

I was pulling my hair out wondering what was going on until I renamed one of the files. 在想重命名其中一个文件之前,我一直想知道发生了什么。

You should check TypeScript typings resolution strategy in the handbook . 您应该在手册中检查TypeScript类型解析策略。

As state there, .ts files have precedence upon .d.ts . 作为状态, .ts文件优先于.d.ts

(just bumped into the same problem myself) (我自己碰到了同样的问题)

For three.js you could skip this local globals chick to chick by using compiler option: 对于three.js,您可以使用编译器选项将此本地全局小鸡跳过到小鸡:

"compilerOptions": {
    "allowUmdGlobalAccess": true,

This will give typescript access to your project global UMD, where three.js is listed when you install it. 这将使打字稿可以访问您的项目全局UMD,在安装时会列出three.js。 Then you could use it at your project same way as for example JQuery. 然后,您可以像在JQuery中一样在项目中使用它。

If this option not available - update your typescript. 如果此选项不可用-更新您的打字稿。

Some details 一些细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM