I am trying to understand how encapsulation works in Typescript and come up with an example which makes me confusing why am I able to access and even change directly the private members of a certain class.
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
I am getting a compile time warning like compilation Error as str2 is private, but still I am able to change it or access it. Am I missing the concept of Encapsulation in general, what it is and how it works on Typescript.