Unlocking the TypeScript UMD global type definition: A step-by-step guide

I have incorporated three@^0.103.0 into my project, along with its own type definitions.

Within my project's src/global.d.ts, I have the following:

import * as _THREE from 'three'

declare global {
    const THREE: typeof _THREE
}

Additionally, in src/global.ts, I have:

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

Furthermore, in src/my-code.js, I am attempting to utilize THREE as a global variable, for example:

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.

The error message states that

'THREE' refers to a UMD global, but the current file is a module. Consider adding an import instead.
.

Upon inspecting the definition of THREE, it redirects me to

node_modules/three/src/Three.d.ts
, rather than my src/global.d.ts file.

It appears that TypeScript is disregarding my global.d.ts declarations?

My tsconfig.json includes the following:

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

Moreover, my global.d.ts resides within the src directory.

If I include

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

at the beginning of my src/my-code.js file (JavaScript), then it functions correctly, allowing me to access the definition of

THREE</code which links to my <code>global.d.ts
file.

Why is it necessary to add the reference comment for it to work?

Answer №1

To incorporate three.js into your project, you can specify the allowUmdGlobalAccess compiler option in your tsconfig.json file:

"compilerOptions": {
    "allowUmdGlobalAccess": true,
}

By doing this, TypeScript will have access to the global UMD of your project, which includes three.js when it is installed. This allows you to utilize three.js in your project in a similar manner as you would with JQuery, for example.

If this option is not available, make sure to update your TypeScript installation.

Additional information can be found here

Answer №2

A simple solution for the issue mentioned above is to rename one of the two global files with the same name. For instance, if you have two files named global.ts and global.d.ts, rename one of them to make it distinct, such as renaming global.ts to make-global.ts while keeping global.d.ts unchanged. This renaming process prevents TypeScript from merging them into one module, thereby resolving any ambiguity when importing them. This strategy helped me avoid confusion and successfully resolve the problem at hand.

Answer №3

For more information on TypeScript typings resolution strategy, be sure to reference the official handbook.

It's important to note that according to the documentation, files with a .ts extension take precedence over files with a .d.ts extension.

(I recently encountered this same issue myself)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the power of nested components within Angular 2

I am encountering an issue with a module that contains several components, where Angular is unable to locate the component when using the directive syntax in the template. The error message I receive states: 'test-cell-map' is not a known elemen ...

What specific files from the Kendo core are required for utilizing Mobile and Angular functionalities?

After browsing through similar questions, I couldn't find a solution. Currently, I am experimenting with Kendo (open source core for now) in a Visual Studio Cordova project. Initially, disregarding Cordova, I am focusing on setting up a basic view wit ...

Monitoring Changes in an Array of Objects with Angular's $watch Feature

Imagine having an array of animal objects linked to the scope. Each object contains a 'name' field and a 'sound' field. After that, I set up a $watch on the array with the objectEquality flag set to true (the third argument). Then, in ...

Utilizing AngularJS: Transforming JSONP information into HTML

I'm relatively new to utilizing $http and fetching data from various websites. My main query is, how can I convert JSONP into HTML? Specifically, when using $http to request the Atari Wikipedia page, the content is displayed with and HTML elements. ...

What is the best way to showcase a QR code on a mesh using three.js?

I utilized the QRcode library available at to try and showcase it on a mesh object as a texture using the following code: var qrcode = new QRCode( "test", { text: "http://jindo.dev.naver.com/collie", width: 128, height: 128, colorDark : " ...

What is the best way to cluster related data points and display them together?

In order to efficiently manage the data I receive, it is necessary to group it based on specific criteria and display these groups in the user interface. "ServiceRequest": [ {"Status": "Re-Open", },{ "Status": "Open", ...

Utilize JavaScript and jQuery to locate a particular character within a string and move it one position back in the sequence

Can you locate a particular character within a string and move it to the position before? For instance: Consider the following string: Kù Iù Mù The desired output is: ùK ùI ùM ...

What is the best way to retrieve the value of a specific textfield using the map function with a collection of

These posts have identical text fields for comments, using the same useState hook. I am trying to extract the value of a specific text field without affecting others. It's similar to Facebook posts, but I'm unsure how to achieve this. const PostT ...

The Eclipse IDE is experiencing issues with the functionality of the JavaScript Number class

In my latest project, I decided to create a Dynamic Web Project using the Eclipse IDE 2019-12 along with Apache Tomcat 8.5.55 server. The main functionality of this project is that a user can input integers and receive their sum as an output. To ensure dat ...

Endless loop in React Native with an array of objects using the useEffect hook

For the current project I am working on, I am facing the challenge of retrieving selected items from a Flatlist and passing them to the parent component. To tackle this issue, I have initialized a local state as follows: const [myState, setMyState] = useS ...

Symfony2: Making AJAX request that unexpectedly returns complete html page

I am facing an issue with setting up a basic AJAX request in Symfony2. It appears that the controller is not receiving the request as expected. Instead of displaying '123', the AJAX response shows the HTML content of the current page (index.html. ...

Verifying the Page Load Status in the Browser

I've been spending a lot of time lately trying to find code that will allow me to trigger an action after the browser finishes loading a page. After much searching, I finally came across this piece of code on <script> var everythingLoaded = s ...

Using a conditional statement to wrap a react Route

Currently, I am facing a challenge while working with react router. Initially, I had two distinct components being rendered under the same route - one with a parameter and one without (this was how the routes were distinguished). Now, my goal is to add opt ...

What are some creative ways to visually distinguish a TextField that is in readOnly mode?

I'm currently working on creating a form using the Material-UI library. I'm having difficulty figuring out how to distinguish my TextField when they are in readOnly mode versus edit mode. At the moment, they appear identical and I would like the ...

Implementing a dual-level hierarchical organization of objects in AngularJS

I am currently developing an Angular application that integrates a cell modifier within a custom cell template in Angular Bootstrap Calendar. Instead of traditional event displays within each cell, I am incorporating sets of tables used for shift sign-ups ...

Converting a "String" value to a specific "Type" in Angular 2 using TypeScript

This is my current object, Home points to the import statement for Home component. import { Home } from '../home/home'; arr = [ { name: "Home", root: Home, icon: "calc" } ]; This is what I want to achieve: import { Home } from & ...

I encountered an issue where the data I passed to a component ended up being undefined

So here's the scenario: I'm working on a Next.js project where I have a context for modals. In this context, I store modal details in an array called modalBase. Additionally, I fetch data from another context (toolsContext) to pass it to componen ...

Encountering a Selection Issue with IE8

I am encountering an issue with my script that involves a form with two select elements for region and state, allowing users to filter states based on the selected region. Everything works smoothly except for one problem - when testing in IE8, I receive th ...

Can the `lang` attribute be used in a `style` tag to specify the CSS preprocessor language for VueJS? Are there any disadvantages to using this method?

Occasionally, I notice people incorporating code like this: <style lang="scss"> ... </style> <style lang="stylus"> ... </style> I checked the documentation for the style tag and found that lang is not a valid a ...

Use RxJS chaining to transform an observable of `File` objects into an observable of strings encoded in base64

I have successfully written code that converts my File object to base64: let reader = new FileReader(); reader.readAsDataURL(myFile); reader.onload = () => { let resultStrOrArrayBuf = reader.result; if (!(resultStrOrArrayBuf ...