Accessing the ‘name’ property of the object’s constructor can help you Get the Class Name of an Object in TypeScript. So how to do it? Let’s go in the detail!
Get The Class Name Of An Object In TypeScript
Method 1: Access the ‘name’ property of the object’s constructor
In TypeScript, we can get the name of an object by accessing the ‘name‘ property of the object’s constructor.
Syntax:
object.constructor.name
In a little more detail, object.constructor returns a reference to the object’s constructor function, which creates the instance object. We then access the ‘name’ property to get the object’s class name.
Take a look at this example:
class Laptop{}
const myLaptop = new Laptop();
const ClassName = myLaptop.constructor.name;
console.log(ClassName );
console.log(Laptop.name);
Output:
"Laptop"
"Laptop"
I have created an instance called myLaptop for the class Laptop. After that, I get the name of the Laptop class and myLaptop object’s class name using these commands in the above example. As you can see, we get the same value in the console. So we got the correct class name of the myLaptop object.
Method 2: Declare a property that stores the value of the class name
If you want to access the object’s name property frequently, declare a property that stores the value of the class name or a method to get the class name.
Example – 2:
class Laptop{
Name = this.constructor.name;
// or you can hard code the class name: Name = 'Laptop'
getName(){
return this.constructor.name;
//or return 'Laptop'
}
}
const myLaptop = new Laptop();
console.log(myLaptop.Name);
console.log(myLaptop.getName());
Output:
"Laptop"
"Laptop"
The approach above will make your code more readable and avoid the repetition of calling object.constructor.name.
Summary
Thank you for being so interested in the content of the article. I hope you understood how we could Get The Class Name Of An Object In TypeScript.
Maybe you are interested:
- Check if Object is instance of Class in TypeScript
- Define an Interface for Array of Objects in TypeScript
- Declare a function with an Object return type in TypeScript

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