0

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.

Mizlul
  • 1
  • 7
  • 41
  • 100
  • That might be invalid Typescript , but it is **valid Javascript** and being as all Javascript can run in Typescript it works but the compiler complains. – Liam Dec 12 '18 at 12:21
  • Possible duplicate of [Why Im able to access private member of the class](https://stackoverflow.com/questions/37997251/why-im-able-to-access-private-member-of-the-class) – Liam Dec 12 '18 at 12:21
  • In Typescript [each member is `public` by default](https://www.typescriptlang.org/docs/handbook/classes.html#public-by-default). – Yom T. Dec 12 '18 at 12:24

3 Answers3

3

The problem is that typeScript just compiles to javaScript, but it doesn't have a runtime.

TypeScript is giving you those warnings at compilation type. It is telling you:

Ey, Encapsulate str2 is private, and you're still trying to access it. You shouldn't do that.

However, once ts compiles and generates the js code, all the typescript annotations are lost in the code. JavaScript does not know anything about private, enum or interfaces. Once your code gets compiled and then runs in a browser or any other js runtime, you'll have an Encapsulate javaScript object with two fields, str1 and str2. They cannot be private or public because js does not know about that. Encapsulate will be a plain bare javaScript object.

So, basically, typeScript helps you catch errors at compile time. But, once the code compiles, it will 'forget' everything about typeScript annotations.

It can tell you that you shouldn't access str2. It can even refuse to compile if such an error is found (this depends upon tsconfig configurations). But the compiled code won't have anything to do with typescript.

Sergeon
  • 6,638
  • 2
  • 23
  • 43
1

This is expected as TypeScript is just a static type checking language. Once compiled to js, the operation you are performing is valid, hence no runtime error

Doğancan Arabacı
  • 3,934
  • 2
  • 17
  • 25
1

Javascript has no concept of private and public members for a class (though this might change in the future). At this time marking a property or function in a class as private is only checked at compile time (this is why you get a syntax error).

Your code will still run though because even though Typescript generated a syntax error it also most likely still outputted valid Javascript code.

Mathyn
  • 2,436
  • 2
  • 21
  • 33