How to Access TypeScript Private Members ?
Last Updated :
15 Apr, 2024
In this TypeScript article, we will learn how to access the TypeScript private members using different approaches. We can interact with the encapsulated components within the class and access them in the application.
There are three different approaches through which we can access TypeScript private members.
Using any Type
In this approach, we are using any Type to access private members. In the example, we have privateVar in the GFGClass which is accessed using any type assertion by bypassing the TypeScript's encapsulation.
Syntax:
let variableName: any = 'This can be any type';
Example: Below is the implementation of the above-discussed approach.
JavaScript
class GFGClass {
private privateVar: string =
'Hey Geek! This is Private Data';
public accessFn() {
return (obj as any).privateVar;
}
}
const obj = new GFGClass();
console.log(obj.accessFn());
Output:
"Hey Geek! This is Private Data"
Using Reflect
In this approach, we are using Reflect to access private members. We have a private member as data2 in the GFGClass2 which is accessed using Reflect.get(this, 'data2'). This approach reflects on the object instance to retrieve the value of the private member.
Syntax:
Reflect.get(target: any, propertyKey: PropertyKey, receiver?: any): any;
Example: Below is the implementation of the above-discussed approach.
JavaScript
class GFGClass2 {
private data2:
string = 'Private Variable accessed using Reflect';
public accessFn() {
const privateVar =
Reflect.get(this, 'data2');
console.log(privateVar);
}
}
const myObject = new GFGClass2();
myObject.accessFn();
Output:
"Private Variable accessed using Reflect"
Using square bracket([]) Notation
In this approach, we are using square bracket([]) Notation to access the private members of the Class. We have defined the private variable as data3 and the private function as privateFn in GFGClass3. We are accessing this member using [] notation and type assertion to any.
Syntax:
const value = (object as any)['propertyName'];
Example: Below is the implementation of the above-discussed approach.
JavaScript
class GFGClass3 {
private data3: string =
'This is Private Variable';
private privateFn(): void {
console.log(
'This is Private Function Called');
}
public accessFn3() {
const privateVar =
(this as any)['data3'];
console.log(privateVar);
const privateFunction =
(this as any)['privateFn'];
privateFunction.call(this);
}
}
const myObject = new GFGClass3();
myObject.accessFn3();
Output:
"This is Private Variable"
"This is Private Function Called"
Using ES6 WeakMap
In this approach, we utilize ES6 WeakMap to access private members. WeakMap allows us to store key-value pairs where the keys are objects and the values can be arbitrary data. We can use WeakMap to store private members and access them indirectly using class methods.
Syntax:
const privateData = new WeakMap();
Example: Below is the implementation of the above-discussed approach.
JavaScript
const privateData = new WeakMap();
class GFGClass4 {
constructor() {
privateData.set(this, { privateVar: 'Private Data using WeakMap' });
}
public accessFn4() {
const privateVar = privateData.get(this)?.privateVar;
console.log(privateVar);
}
}
const myObject = new GFGClass4();
myObject.accessFn4();
Output:
Private Data using WeakMap
Similar Reads
How to implement class constants in TypeScript ? In this article, we will try to understand how to create several class constants (properties with constant values) in TypeScript with the help of certain code examples for better concept understanding as well as clarification. Let us first understand quickly how we may create a class in TypeScript w
3 min read
How to Import Another TypeScript Files? To use code from one TypeScript file in another, we use the module system introduced in ECMAScript 2015. This allows us to export functions, classes, or variables from one file and import them into another. By doing this, we can reuse code from previous projects without having to rewrite it. This ma
4 min read
Access Modifiers in TypeScript In TypeScript, access modifiers control the visibility and accessibility of class members, such as properties and methods, aligning with the principles of encapsulation and information hiding in object-oriented programming.Public: Members are accessible from anywhere; this is the default modifier if
4 min read
TypeScript - Access Modifiers and Readonly Properties In TypeScript, access modifiers and readonly properties help define how class members (properties and methods) can be accessed and modified. Access modifiers control the visibility of a class member, determining where it can be accessed from. Readonly properties, on the other hand, ensure that a var
5 min read
How to Access Enum Values in TypeScript ? Enums are a feature in TypeScript that help organize collections of related values. Accessing these values efficiently is crucial for clean, maintainable code. This guide provides a straightforward overview of different methods to access enum values in TypeScript, including bracket notation, dot not
3 min read