DeepImmutable does not guarantee Immutable

After looking at all the codes of babylon.js,
I found that the DeepImmutable type exists just to support Immutable for the container type related to math(e.g Vector3).

But DeepImmutable is actually not fully ensure a immutable. Because a method exists to modify self.

for example:

const vec: DeepImmutable<Vector3> = new Vector3(); // create immutable vector3

vec.set(1, 2, 3); // vec modified by method

To solve this problem. I think it is desirable to implement Immutable by creating a separate Readonly interface.

This is the ideal code I think:

export type ImmutConvertable<T = any> = T & {
    freeze(): T;

export type Immutable<T extends ImmutConvertable> = 
    T extends ImmutConvertable<infer U>
        ? U
        : never;

 * Ensure complete Readonly
 * by putting only the members you want to expose into the interface.
interface ReadonlyVector3 {
    readonly x: number;
    readonly y: number;
    readonly z: number;

class Vector3 implements ImmutConvertable<ReadonlyVector3> {
    public x: number;
    public y: number;
    public z: number;

    public constructor(x: number, y: number, z: number) {
        this.x = x;
        this.y = y;
        this.z = z;

    public freeze(): ReadonlyVector3 {
        return Object.freeze(this);

    public set(x: number, y: number, z: number): Vector3 {
        this.x = x;
        this.y = y;
        this.z = z;
        return this;

    public copy(other: Immutable<Vector3>): Vector3 {
        this.x = other.x;
        this.y = other.y;
        this.z = other.z;
        return this;

type A = Immutable<Vector3>;
//   ^---type A is evaluated to `ReadonlyVector3`

If I receive a positive response to this design change, I can implement it.

I am not sure freezing would work as it would prevent other usage elsewhere to modify the content.

DeepImmutable here is mostly readonly but I aggree it does not remove the function modifying the content.

In that code, freeze is just dummy data for inference

The important thing is that if you create the Readonly version of the interface yourself, it can be completely unmodifiable.

Hi and welcome to the Community,
Your first post and your pseudo sure makes me wonder (no offense here). I just find it funny that a Guy with the name of @noname0310 kicks in on his first post on the forum to spot on what I believe to be (deeply) governance related stuff. I sure do hope you are not a hacker, a grey hat or a spy, are you? :grin: :wink: And then, I hope you are doing this for good? :angel:
May I just ask what are your expectations with BJS and why do you want to challenge the ‘immutable’ status? Just out of curiosity, of course. :thinking: You don’t really have to answer if you don’t want to.
Else, as I said, welcome to the Community. Such post for a first post is quite uncommon, would you agree? I find it both a bit disturbing (as explained above) but also interesting. May be we could really use your skills to make BJS even stronger and true to its philosophy… Meanwhile, have a great day :sunglasses:

this complete immutable guarantee is already being implemented in libraries like JOML,
The way I suggest it is a more advanced way of utilizing the typescript type system,
and I just want a lot of people to use it.

A nice way to elude my question about your motives :wink:. Fair enough. As I said, you don’t have to answer if you don’t wish to.

I wasn’t challenging the tech aspect and then, it’s certainly not mine to do so. I’m sure the team will consider your suggestion (they already started) and I’m also sure you will get feedback on it. However, the fact that it’s used in other libraries (no matter the quality of these) doesn’t necessarly mean it’s the way to go for BJS. Again, not mine to say.

You only cought my interest because of the topic and content of your first post. In this forum, people would usually share about their background and motives first (at least a little). We are a small but committed ‘extended family’ and I believe we all like to know a little about the people that are joining, especially when it comes to discussing sensitive aspects. But then again, no obligation here.

Thanks for the reading and I hope you are enjoying your experience of BJS as much as I do :smiley:

As a developer if I see something like DeepImmutable I’m assuming it’s doing what it says to - that you can’t modify instance’s properties.
As @noname0310 has shown - now it is possible to modify those properties :slight_smile: