爲什麼我必須將類型定義從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。我更新了問題並用實際代碼替換了圖片。 –