Why is it necessary to modify the type definition from material: Material | Material[];
to material: Material;
in order to resolve the error specified below? Despite explicitly setting the material
parameter to Material, TypeScript seems to be assuming that it is of type Material[]
. What could be causing this discrepancy?
Error Encountered in Typescript/ThreeJS:
this.obj3D.traverse((child) => {
if (child instanceof THREE.Mesh) {
// Error occurs at the line below:
// Property 'shading' does not exist on type 'Material | Material[]
child.material.shading = THREE.SmoothShading;
child.material.side = THREE.DoubleSide;
child.scale.set(this.scale, this.scale, this.scale);
child.castShadow = this.castShadow;
child.receiveShadow = true;
child.material.needsUpdate = true;
}
});
Definition in ThreeJS:
export class Mesh extends Object3D {
constructor(geometry?: Geometry, material?: Material | Material[]);
constructor(geometry?: BufferGeometry, material?: Material | Material[]);
geometry: Geometry | BufferGeometry;
material: Material | Material[]; // Modification made by removing *| Material[]* here
drawMode: TrianglesDrawModes;
setDrawMode(drawMode: TrianglesDrawModes): void;
updateMorphTargets(): void;
getMorphTargetIndexByName(name: string): number;
raycast(raycaster: Raycaster, intersects: any): void;
}