Loading

TypeScript

Difference between private keyword and private fields in TypeScript. The Complete TypeScript Developer Course 2023 [Videos].

TypeScript 3.8 supports the private keyword to declare its members private. And the private fields in TypeScript are the ones newly proposed for JavaScript. These fields are preceded by #.

Private keyword: The private keyword in TypeScript is used to mark a member private which makes it inaccessible outside the declared class. The fields are preceded by the private keyword to mark them private.

  • Syntax:
    private variableName
  • Example:

    class letters { 
        private a;
      
        constructor(alphabet){ 
            this.a = alphabet;
        
     
        getA(){
            return this.a;
        }
    const l = new letters("gfg"); 
    console.log(l.getA()); 
  • Output:
    gfg

Private fields: Private fields in TypeScript are the ones that have been newly added to JavaScript, the members to be declared private are preceded by a #. Private members cannot be accessed outside their class.

  • Syntax:
    #variableName
  • Example:

    class letters {
        #a;
     
        constructor(alphabet){
            this.#a = alphabet;
        }
     
        getA(){
            return this.#a;
        }
    }
    const l = new letters("gfg");
    console.log(l.getA());            

    Output:

  • gfg

Hence, to implement the private keyword in runtime when compiling TypeScript to ESNext JavaScript one must consider using the private syntax # defined for JavaScript.

Difference between private keyword and private fields:

Private keywordPrivate fields
It is a TypeScript modifier.It is a newly added syntax by ECMA to JavaScript, which is also available in TypeScript.
It is a compile time annotation. So, when a transpiler converts TypeScript code to JavaScript, the private keyword is removed.The private fields remain private at runtime even after the TypeScript code gets converted to JavaScript by the transpiler.

See All

Comments (167 Comments)

Submit Your Comment

See All Posts

Related Posts

TypeScript / Blog

Difference between TypeScript and JavaScript

When JavaScript was developed the JavaScript development team introduced JavaScript as a client-side programming language. But when people were using JavaScript then developers get to know that JavaScript can be used as a server-side programming language also. But When JavaScript was growing then the code of JavaScript became complex and heavy.
19-jan-2022 /33 /167

TypeScript / Blog

What is the difference between interface and type in TypeScript ?

Both the methods Type and the Interface are used to describe the structure of the objects in TypeScript. But holds some specific features that will be helpful according to the situation, choosing between them and totally depends on the developer.
/33 /167

TypeScript / Blog

What is the Function type in TypeScript ?

TypeScript is a JavaScript-based programming language with a typed syntax. It provides improved tools of any size. It adds extra syntax to JavaScript. This helps in facilitating a stronger interaction between you and your editor. It also helps in catching the mistakes well in advance.
19-jan-2022 /33 /167