为什么我必须将类型定义从material: Material | Material[];
修改为material: Material;
才能解决下面详述的错误? TypeScript看似假设material
参数的类型为Material[]
,即使我明确将其设置为“材质”。我错过了什么吗?为什么我的参数假设是另一种类型的?
打字稿/ ThreeJS错误:
this.obj3D.traverse((child) => {
if (child instanceof THREE.Mesh) {
// Error in 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;
}
});
ThreeJS类型定义:
export class Mesh extends Object3D {
constructor(geometry?: Geometry, material?: Material | Material[]);
constructor(geometry?: BufferGeometry, material?: Material | Material[]);
geometry: Geometry | BufferGeometry;
material: Material | Material[]; // Had to delete *| Material[]* to fix
drawMode: TrianglesDrawModes;
setDrawMode(drawMode: TrianglesDrawModes): void;
updateMorphTargets(): void;
getMorphTargetIndexByName(name: string): number;
raycast(raycaster: Raycaster, intersects: any): void;
}
请阅读[为什么不上传图片的代码时提问?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so - 当问一个问题) – Rabbid76
感谢您指出@ Rabbid76。我更新了问题并用实际代码替换了图片。 –