TypeError: Cannot set property visibility of [object Object] which has only a getter

Babylon.js Playground — here all is ok, but when I run this code on my localhost I got error:

TypeError: Cannot set property visibility of [object Object] which has only a getter

How it posiible? Maybe I forgot some import?

import { ArcRotateCamera } from '@babylonjs/core/Cameras/arcRotateCamera';
import { Engine } from '@babylonjs/core/Engines/engine';
import { HemisphericLight } from '@babylonjs/core/Lights/hemisphericLight';
import { SceneLoader } from '@babylonjs/core/Loading/sceneLoader';
import { Vector3 } from '@babylonjs/core/Maths/math.vector';
import { Scene } from '@babylonjs/core/scene';

import '@babylonjs/core/Loading/loadingScreen';
import '@babylonjs/loaders/glTF/2.0/glTFLoader.js';

import { css, html, LitElement } from 'lit';
import { customElement, property } from 'lit/decorators.js';

All package versions are latest:

{
  "dependencies": {
    "@babylonjs/core": "^5.54.0",
    "@babylonjs/gui": "^5.54.0",
    "@babylonjs/inspector": "^5.54.0",
    "@babylonjs/loaders": "^5.54.0",
    "lit": "^2.7.2",
    "typescript": "^4.9.5"
  },
  "devDependencies": {
    "browser-sync": "^2.29.1",
    "es-dev-server": "^2.1.0",
    "rollup": "^2.79.1",
    "rollup-plugin-node-resolve": "^5.2.0",
    "rollup-plugin-terser": "^7.0.2",
    "rollup-plugin-typescript2": "^0.34.1"
  }
}

Hi, some of your meshes are InstancedMesh, and on an InstancedMesh you can’t set the visibility, only get

And this works in playground because … javascript :slight_smile:
If you create a pg with typescript you’ll get the same error

1 Like

So, there are should be a TypeScript error in this case right in the IDE, not in runtime. As I understand, the InstancedMesh should be readonly. And what is reason? As we can see in my playground example it works well.

Check the pg I made with typescript and uncoment line 23 to get the same error like you say, but in your pg made with js there is no error because
we don’t have readonly keyword in js. You can set and get anything in js

I hope I understood right and I didn’t say something stupid here :sweat_smile:

Actually, you will get an error at runtime if you try to set a property which has no getter only in “strict mode”.

Try this code in the console of a browser:

let objTest = {
  get visibility() {
    return 1;
  },
};
objTest.visibility=0;

and:

"use strict";
let objTest = {
  get visibility() {
    return 1;
  },
};
objTest.visibility=0;

I talking about TS error, seems it should be an error here:
image
no errors here

And it has if I define possible type in forEach:
image

So, it looks like the scene.meshes has irrelevant type — in fact it can be also is InstacedMesh with readonly visibility, but current types does not pass this case.

AbstractMesh is a superclass of InstancedMesh so the typing isn’t “irrelevant”, it’s just considering the more general case. In any case, I wonder if we should define a setter with a warning on InstancedMesh to avoid an error? @RaananW

Oh, what an interesting case.
Let me think about it :slight_smile:

2 Likes

Adding no-op setters to instancedMesh by RaananW · Pull Request #13744 · BabylonJS/Babylon.js (github.com)

3 Likes