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.