Tips on how to specify a reference to a pre-existing namespace that can be accessed from the JavaScript bundle while it is

I am in the process of developing a plugin for an existing JavaScript application called Forge Autodesk.Viewing.

Recently, they have integrated THREE.js into their app bundle starting from version 6.

Currently, I am able to utilize it within my plugin by using the following code snippet:

declare var THREE:any; 

However, I lose all types with this approach, so I decided to install three.js separately using npm:

npm install --save three

Now, I can use THREE and import it, but importing is unnecessary since it is already included in the main app. What I really need is to reference the types, which led me to try this:

    declare var THREE:THREE;
//Cannot use namespace 'THREE' as a type.

After that, I attempted:

/// <reference types='three' />
, which worked fine, but:

        const planes:THREE.Plane[] = []; //this line is fine

        planes.push(new THREE.Plane()); //but this gives error
        
        //'THREE' refers to a UMD global, 
        // but the current file is a module. 
        // Consider adding an import instead.

The TypeScript compiler insists on importing it like this:

import * as THREE from 'three'; 

Although it compiles successfully, when I run the app, it crashes because it attempts to load another instance of THREE.js, which is not necessary as it is already present in the main application.

Is there a way to correctly declare the reference and maintain types for a namespace that is available in the main JavaScript application?

Answer №1

If you're working with Forge Viewer, make sure to check out the TypeScript definition file (.d.ts) that is compatible with a THREE.js definition file. You can find more information about it here: .

Answer №2

To utilize the THREE module in your project, follow these steps:

import * as THREE from 'three'; // or import THREE from 'three';

Alternatively,

var THREE = require('Three').

Make sure to use webpack or any other module loader!

If you prefer manually including the THREEJS distribution file and using TS bindings without modules (not recommended), you can include the Three.d.ts file along with other THREEJS d.ts files in your project. Then, reference it like this:

/// <reference path="..\..\node_modules\@types\three\index.d.ts" />

Note: Avoid importing the THREE namespace with "import" or "require" in this scenario.

Answer №3

If you encounter the following issue:

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

(learn more about UMD)

You can resolve this by modifying your tsconfig.json:

"compilerOptions": {
    "allowUmdGlobalAccess": true,

(explore other config options)

This setting grants the compiler access to the UMD global, eliminating the need for importing or referencing certain modules in that scenario.

This is particularly relevant with three.js as they have already included the THREE namespace as a module in the UMD global scope. If you wish to include this module, you should import it. However, if you only need to reference it, you can utilize this option. If TypeScript does not recognize this configuration option, simply update your TypeScript version.

npm install typescript

Special thanks to SalientBrain and Petr Broz for their assistance and guidance.

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

The argument for the e2e test is invalid because it must be a string value

Currently, I am conducting an end-to-end test for an Angular application using Protractor. However, I encountered an error while running the test for a specific feature file. The UI value that I am checking is - WORKSCOPES (2239) Below is the code snippe ...

Ensuring seamless collaboration between Typescript node modules

Is there anyone who has successfully set up a configuration where module 1, using TypeScript, is referencing another module 2 also with TypeScript, and both are utilizing tsd types like node.d.ts? I have been able to compile and use both modules without a ...

Nuxt middleware failing to verify user's logged-in status

I am currently working on implementing user authentication and redirection logic based on the user's authentication status. For instance, if a logged-in user tries to access the login or signup page, they should be automatically redirected. To achieve ...

Having trouble loading the jade view in express

I am having trouble with creating a jade view and loading it using express. The path / works fine, but when I try to load helloworld, the browser returns an error saying Cannot get /helloworld. This is the view I have created and saved in the views folder ...

Distinguishing between Javascript Array Objects and Array-Like Objects: An Elucidation

While attempting to convert an array-like object into a string using JSON.stringify, I discovered that the function was not working correctly when the array-like object was declared as an array object. For more clarity, please refer to the following --> ...

Steps to creating a Partial Pick attribute

I'm facing an issue with a function I have function someThing(someArg: Pick<HTMLElement, 'id' | 'style'>) {...} The problem is that I only want to apply certain styles to someArg.style, not all the styles from CSSStyleDecla ...

Is it possible to close the menu by clicking outside a div or letting it disappear on mouseout after a set period of time?

When visiting a website, you may notice menus that display sub-menus when hovered over. These menus and sub-menus are created using div elements, with the sub-menus hidden initially using style.display = none; The issue arises when a sub-menu is opened an ...

Error in TypeScript: The type 'Color' cannot be assigned to the type '"default" | "primary" | "secondary"'

Currently, I am utilizing MUI along with typescript and encountering the following issue. It seems like I may be overlooking a fundamental concept here but unfortunately cannot pinpoint it. Error: Type 'Color' is not assignable to type '&quo ...

Incorporate typings into your CDN integration

I'm interested in utilizing a CDN to access a JSON validation library, as it's expected to provide faster performance (due to retrieving the file from the nearest server within the CDN). The JSON validation library in question can be found here: ...

Vue not triggering computed property sets

As per the guidelines, I should be able to utilize computed properties as v-model in Vue by defining get/set methods. However, in my scenario, it isn't functioning as expected: export default{ template: ` <form class="add-upload&quo ...

Troubleshooting issue: Dropdown menu malfunctioning after using replaceWith() and appendTo()

I could really use some assistance. Admittedly, my knowledge of php, js, jquery, and similar languages is limited. I am currently working on a small web application where users fill out a form with specific requests, the data is then stored in a database, ...

Is it possible to establish a scope for jquery.ajaxSetup()?

Currently, I am working on a project involving numerous ajax calls with repetitive parameters. After some consideration, I am contemplating using jquery.ajaxSetup() to streamline the code. However, jQuery does not recommend this approach in their API docu ...

What is the best way to choose a tag from an app within components or views?

In my main component file, I added a color picker input in the navigation section to change the background color of the application. This works fine as the body tag can be accessed easily within the DOM. Furthermore, I store the selected color in local st ...

Issue encountered with my AJAX request in JavaScript on Internet Explorer 8

Check out the range of products available on this website using jQuery When using Firefox or Safari, clicking on view will show you more product details on the right side. This could include a gallery with multiple images, downloadable spec sheets if av ...

Find all documents in Angular Firestore that are related to a specific document_REFERENCE

Seeking a way to search through all documents in collection a that have a reference to a specific document in collection b. Despite my efforts, I couldn't find a solution here or on Google :/ This is what I tried in my service class: getAsFromB(id ...

How can I display 4 react components, such as custom buttons, in a manner that ensures the most recently pressed button appears on top?

I've been attempting to solve this problem, but I'm struggling to find a solution. My current approach involves grouping the 4 button components in an array and shifting their positions based on user input. Is there a more efficient way to accomp ...

Implement a button transformation upon successful completion of a MySQLi update using AJAX

When displaying multiple database results with buttons that can be turned on or off inside a div, I am looking to implement AJAX to toggle the button state between ON and OFF upon clicking, and then update the button without refreshing or reloading the ent ...

When Gulp is executed it throws an error that is not handled: `error` event is unhandled

Check out the code below: main.js var React = require('react'); var App = require('./components/app.js'); React.render( <App />, document.getElementById('container') ); app.js var React = require(' ...

Can the page's user interface stay the same even after refreshing the page?

Are you considering including a test case to verify if all checkboxes are unchecked after the page reloads? Is it possible for checked checkboxes to remain selected even after reloading or navigating away and returning to the page? Could this issue be due ...

What is the best way for me to bring in this function?

Currently, I am in the process of developing a point-of-sale (POS) system that needs to communicate with the kitchen. My challenge lies in importing the reducer into my express server. Despite multiple attempts, I have been unable to import it either as a ...