In this article, you can find the answer to the error “Property is private and only accessible within class” in TypeScript. Check out the information.
What does this error mean?
In TypeScript, setting a private property will make that property less accessible when you declare a class. That means you can’t access it outside the scope of the class.
class Student {
name: string = 'Jason';
age: number = 24;
private phoneNumber: string = '088886666';
}
const student = new Student()
console.log(student.phoneNumber)
Output:
Property 'phoneNumber' is private and only accessible within class 'Student'.
As you can see, the Student class is created with three properties: name, age, phoneNumber. Among them, phoneNumber is a private property, so we cannot access it outside of the class scope.
Methods for the error “Property is private and only accessible within class” in TypeScript
Here are some solutions to this problem. Let’s see what they are.
Set that property to public
The simplest way is to set the public scope for the property. When set to public, you can access that property anywhere.
class Student {
name: string = 'Jason';
age: number = 24;
public phoneNumber: string = '088886666';
}
// create an instance of class Student
const student = new Student()
// direct access to property phoneNumber
console.log("The phone number: " + student.phoneNumber)
Output:
"The phone number: 088886666"
If we don’t use the scoping keyword when declaring the property, the declared property will default to public scope. Like two properties: name and age are implicitly declared public even though we don’t use the ‘public‘ keyword when declaring them.
Define a method to access the private property
We can still access the private property through a class method with this approach.
Take a look at this example:
class Student {
name: string = 'Jason';
age: number = 24;
private phoneNumber: string = '088886666'
getPhoneNumber(){
return this.phoneNumber
}
}
// create an instance of class Student
const student = new Student()
//get the value of property phoneNumber through the class method
console.log("The phone number: " + student.getPhoneNumber())
Output:
"The phone number: 088886666"
The property phoneNumber is scoped to private. However, the method getPhoneNumber is defined to return the phoneNumber’s value.
We can still execute the program because the method getPhoneNumber is in public scope and is defined in the same class Student with property phoneNumber. We can consider getPhoneNumber as an intermediate to get the value of phoneNumber.
Summary
In conclusion, we showed you how to fix the error “Property is private and only accessible within class” in TypeScript. I hope these methods will help you in your work. If you have any questions or problems, please comment below. I will answer as possible. Thanks for reading!
Maybe you are interested:
- Expected 0 arguments, but got 1 error in TypeScript
- Yield expression implicitly results in an ‘any’ type in TypeScript
- Catch clause variable type annotation must be any or unknown if specified

My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.
Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js