Site Overlay

What's the use of TypeScript 3.8/ECMAScript Private Fields?

0

Hi devs!
TypeScript 3.8 has added support for ECMAScript Private Fields but I’m not sure why do we need them since TypeScript has already private variables?
 

February 23, 2020
1
0

TypeScript did support a private keyword but it doesn’t really make the field private. This is just for the compiler, but you can access in plain JavaScript. In the other hand, the Ecmascript Private Fields makes it truly private, which means you can’t access it.

This is an example of a Ecmascript private field in typescript:

 

class Customer {
    #email: string

    constructor(email: string) {
        this.#email = email;
    }

    getEmail() {
        return this.#email;
    }
}