How to get the class name of an object at runtime in TypeScript?
Hello! I'm writing a TypeScript application that handles serialization of different entities. I have a class hierarchy, and I need to determine at runtime which specific class an object is an instance of, in order to process it correctly.
For example, I have a base class `Shape` and two subclasses: `Circle` and `Rectangle`. I want to write a function that takes an array of shapes and outputs the type of each one. In JavaScript I would use `obj.constructor.name`, but in TypeScript this property may be missing or undefined after minification/bundling. Moreover, TypeScript is strictly typed, and simply accessing `constructor` doesn't always give the expected result.
Here's simplified code that doesn't work as I expect:
```typescript
class Shape {
No answers yet.
Log in to leave an answer
Blogs
Introduction to Go: Where to Start in 2025 — A Beginner's Guide
Import cycle not allowed in Go: How to Break the Vicious Circle
PHP HTTP Client: Making Requests Correctly from A to Z
Logging in PHP Applications: Best Practices for Beginners