I'm diving into the world of encapsulation in Typescript and stumbled upon an example that has left me scratching my head. I am confused as to why I can access and modify the private members of a specific class directly.
class Encapsulate {
str:string = "hello"
private str2:string = "world"
}
var obj = new Encapsulate();
console.log(obj.str); //accessible
obj.str2 = "something else";
console.log(obj.str2); //compilation Error as str2 is private
OUTPUT: hello something else
Although I receive a compile time warning stating that there is a compilation error because str2 is private, I am still able to alter it and access it. This makes me question whether I truly understand what encapsulation is and how it operates in Typescript.