Interfaces still baffle me a bit. I understand that interfaces are typically used for public properties, but I want to create an interface that will prevent access to uninitialized properties. Currently, I am able to access this.material
without any errors even when it is undefined
.
This is my current code:
interface ColliderConstructorArgumentInterface {
radius: number,
position?: THREE.Vector3
}
export interface Collider extends ColliderConstructorArgumentInterface {
geometry: THREE.SphereGeometry
material: THREE.MeshBasicMaterial
mesh: THREE.Mesh
position: THREE.Vector3
}
export class Collider {
constructor(settings: ColliderConstructorArgumentInterface) {
this.radius = settings.radius;
this.position = settings.position ? settings.position : new THREE.Vector3(0, 0, 0);
this.geometry = new THREE.SphereGeometry(this.radius, 8, 8);
this.mesh = new THREE.Mesh(this.geometry, this.material);
console.log(this);
}
}
I have two interfaces - one for the class constructor arguments and another for the other class properties.
However, as you can see, I am using the property material
while setting up this.mesh
, even though I haven't initialized it in the constructor, and I'm not receiving any error or warning.
...
How can I modify my interfaces to receive an error message?
I want my code to be safe and clean.
...
Thank you for any suggestions!