Three.js: Cannot set userData property of threejs MeshBasicMaterial instance at instantiation time

Created on 4 Nov 2020  Â·  2Comments  Â·  Source: mrdoob/three.js

Describe the bug

I get a TypeScript error when attempting to set the userData property of a MeshBasicMaterial instance at instantiation time, even though this property should be available from the parent Material class or MaterialParameters interface. I do not get an error when setting this property after instantiation.

To Reproduce

I am running three.js (r122) installed via npm:

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

This returns the following error:

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

Expected behavior

I should be able to specify userData at instantiation time, as MeshBasicMaterial inherits from Material, which should offer the userData property (perhaps via the MaterialParameters interface?).

As a workaround, the following did not yield an error:

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

Platform:

  • Device: Desktop
  • OS: MacOS
  • Browser: Chrome
  • Three.js version: r122
TypeScript

Most helpful comment

Sure, I can do that.

All 2 comments

The TypeScript types are maintained in .d.ts files — Would you want to make a pull request to add this field below?

https://github.com/mrdoob/three.js/blob/95ef309c79158aa506f3f7ddeab769e7c4908187/src/materials/Material.d.ts#L16-L24

Sure, I can do that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

filharvey picture filharvey  Â·  3Comments

danieljack picture danieljack  Â·  3Comments

seep picture seep  Â·  3Comments

ghost picture ghost  Â·  3Comments

akshaysrin picture akshaysrin  Â·  3Comments