Site Overlay

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

Share : Answer for What's the use of TypeScript 3.8/ECMAScript Private Fields?

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;
    }
}