The userData property cannot be set on an instance of the threejs MeshBasicMaterial

Encountering a TypeScript error while trying to assign the userData property of a MeshBasicMaterial instance when creating it.

Here is an example:

import * as THREE from 'three';
...
const customUserData = {
  ...
};
const myMaterial = new THREE.MeshBasicMaterial({
  color: 0xf7f7f7, 
  transparent: true, 
  opacity: 1, 
  side: THREE.BackSide,
  userData: myCustomUserData
});

This results in the following error message:

Argument of type '{ color: number; transparent: true; opacity: number; side: THREE.Side; userData: null; }' is not assignable to parameter of type 'MeshBasicMaterialParameters'.
  Object literal may only specify known properties, and 'userData' does not exist in type 'MeshBasicMaterialParameters'.  TS2345

Upon further examination of the type definitions, MeshBasicMaterial extends Material:

export class MeshBasicMaterial extends Material { ... }

The Material class includes a userData property:

export class Material extends EventDispatcher {
    // ...
    userData: any;
    // ...
}

Interestingly, setting the userData property after instantiating the class works:

let myMaterial = new THREE.MeshBasicMaterial({...});
myMaterial.userData = myCustomUserData;

Shouldn't MeshBasicMaterial have inherited the userData property from Material? Or is there something else at play here related to the MeshBasicMaterialParameters type? The reason for this behavior is not entirely clear.

Answer №1

It seems you've stumbled upon a missing type definition!

You are correct in noting that the Material class definition file includes a .userData property. This indicates that setting it as material.userData = xxx; should function properly.

However, when initializing the material with new THREE.Material(props);, the structure used for the props argument is slightly different and defined in the interface MaterialParameters. If you examine what this includes on line 16 from the source code, you'll notice that userData is absent.

This issue also affects MeshBasicMaterialParameters since it inherits from MaterialParameters.

A temporary fix would be to manually set it using material.userData = xxx;, while also raising a Github issue for a potential resolution in upcoming releases. For a more complex solution, consider creating your own declaration file and performing a Merge operation with the MaterialParameters interface to incorporate .userData.

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

What is the way to create a `Partial<T>` specifically for nullable fields?

I have a requirement for a custom Partial<T> type that can transform nullable fields into optional ones. Currently, I am focusing on typing our ORM, which converts undefined values to null for nullable fields. In particular, I want to modify a type ...

In TypeScript, the 'onChange' is declared multiple times, therefore this particular usage will be scrutinized carefully

Within my React project, I am utilizing material-ui, react-hook-form, and Typescript. However, I encountered an error in VSCode when attempting to add the onChange function to a TextField component: 'onChange' is specified more than once, resul ...

Is Webpack CLI causing issues when trying to run it on a .ts file without giving any error

I am facing an issue with my webpack.config.js file that has a default entrypoint specified. Here is the snippet of the configuration: module.exports = { entry: { main: path.resolve('./src/main.ts'), }, module: { rules: [ { ...

Do constructors in TypeScript automatically replace the value of `this` with the object returned when using `super(...)`?

I’m having some trouble grasping a concept from the documentation: According to ES2015, constructors that return an object will automatically replace the value of “this” for any instances where “super(…)” is called. The constructor code must ...

Finding the hidden treasure within a nested array and transforming a boolean gem into a dazzling string - the ultimate quest!

How do I convert the values in the "active" field to "Yes" if true and "No" if false? [ { "id":81, "time":"2022-01-01 19:30:00", "subList":[ { "active":false, "success":null } ] }, { "id":89, "time":"2022-01-01 21:00:15", "subList":[ { "active":true, "suc ...

Error in Typescript SPFx: The property 'news' is not found in the type 'Readonly<{}>'

Currently, I am working on developing a spfx react component to showcase an RSS feed in the browser. My prototype is functional in a test environment, however, as spfx utilizes TypeScript, I am encountering a type error that I am unsure how to resolve. Rs ...

Creating a dropdown menu in Bootstrap 5 without using any of the Bootstrap

In my Angular application, I have a header with icons and pictures that I would like to use as dropdown menus. The code snippet for this functionality is shown below: <li class="nav-item dropdown"> <a class="nav-li ...

Developing a custom typography attribute for themes in Material-UI with TypeScript

I have encountered an issue with my code where the palette works fine, but there seems to be a problem with the typography section without any errors being thrown: Here is a breakdown of the steps I took: Firstly, I imported the module import "@mui ...

Utilizing Router Outlet in Angular to Access API Data

I've encountered an issue where I can't pass parent data from the ngOnInit route params to my child component, user-seminar. After some research and searching on Google, I found a solution involving services. To address this problem, I modified ...

Encountering an error of "undefined" when attempting to access loop variables within the ngOnInit() lifecycle hook

When I call the ngOnInit() function, data is fetched from a service and stored for use in my component. However, I am encountering issues with accessing this data outside of the subscribe method. I am unsure of what the problem might be. import { Componen ...

While performing compilation, Angular's ngFor triggers an error when used with SVG elements

I am attempting to create a recursive function of lines in order to generate a graph, but I am encountering a strange error in the console. It works fine on node.js. Below is the code snippet: <svg height = "200" width = "300"> ...

Exporting the interface for the state of the redux store

After setting up a redux module, I have organized the following files: //state.tsx export default interface State { readonly user: any; readonly isLoggedIn: boolean; } //types.tsx export default { REQUEST: 'authentication/REQUEST', SUC ...

Incorporate an imported type as a nested component within an interface, and subsequently utilize the interface as properties within the Vue3 script

While this may appear to be a straightforward question, I have not been able to find any discussions on this topic. Currently using Vue3 with script setup Objective: In short: I am attempting to utilize a child type definition to specify one key of an in ...

What is the best way to retrieve the value from a Material UI textfield after hitting the enter key

Having trouble retrieving input values with the provided code. Attempted using onKeyUp, onKeyDown, and onKeyPress, but none of them returned the value as desired. Typically, I would use the onChange property to get the value, but it triggers for every ne ...

The pathway of external component template or css during the TypeScript compilation process to the outDir destination

When developing Angular 2 applications, I often run into an issue with my tsconfig.json file. In this file, I have set the parameter as follows: "outDir": "dist" This configuration instructs the TypeScript-to-JavaScript compiler to save the compiled file ...

The GLTFExporter exports GLTF files with a shade of gray

I recently exported a glTF file using this demo page. However, when I imported the file into Blender, the 3D model appeared in grey color and seemed to be missing color information. Does anyone know how to rectify this issue? Could it be that glTF files d ...

I'm having trouble with my input - it's not recognizing the text I'm typing or capturing the value. What could be

There seems to be an issue with the input box in my code that uses react hook form. Specifically, when I type into the input field with the placeholder text "Or search for a contact type by name...", the characters do not appear as expected. I have tried ...

Find keys in an array based on a specified value

I need to retrieve an array of keys from an object that match a specified value ...

When working with Typescript, it's important to handle errors properly. One common error you might encounter is: Error:(54, 33) TS2686: 'fabric' refers to a UMD global, but the current file is a module

Encountering an Issue: import {Canvas} from "fabric"; Error:(54, 33) TS2686:'fabric' refers to a UMD global, but the current file is a module. Consider adding an import instead. In my Angular project with TypeScript, I am using fabric which ...

What could be causing the decrease in speed of my Three.js animation within Vue.js?

I attempted to replicate the impressive wave simulation from this CodePen link: https://codepen.io/cheekymonkey/pen/vMvYNV, using Vue.js. However, the animation seems to be running significantly slower when implemented in Vue.js. In my effort to recreate ...