Mesh material.diffuseColor does not exist on type 'Material'

It doesn’t seem possible to access a material’s diffuseColor via its mesh.

Is this correct/defined behaviour?

Correct, this is expected.

box.material is typed as the base BABYLON.Material, which doesn’t have diffuseColor property.

Use one of these patterns:

  1. Narrow with instanceof(safe)
    if (box.material instanceof BABYLON.StandardMaterial){
    box.material.diffuseColor = new BABYLON.Color3(1,0,1);
    }

  2. Explicit cast (only if you are sure it is BABYLON.StandardMaterial)
    (box.material as BABYLON.StandardMaterial).diffuseColor = new BABYLON.Color3(1,0,1);

I just realised the code I was looking at was TypeScript. D’oh!

You don’t have to cast it at all if you just do it as a defined variable then assign it to the material of the object, it will infer it’s type.

const mat = new StandardMaterial(...)
mat.diffuseColor = …
mesh.material = mat

Will work, what is happening is the class of a Mesh has its material prop set to the base class for Material since it needs to accept all extension classes of Material (Standard, PBR, Shader etc) so when you insatiate the material directly onto the object it sees its type as just that base Material class since its inferring it from the Mesh class.