Categories
TypeScript Answers

How to check if a specific object is empty in TypeScript?

Spread the love

Sometimes, we want to check if a specific object is empty in TypeScript

In this article, we’ll look at how to check if a specific object is empty in TypeScript.

How to check if a specific object is empty in TypeScript?

To check if specific object is empty in TypeScript, we can use the Object.keys method.

For instance, we write

class Brand {}
const brand = new Brand();

if (Object.keys(brand).length === 0) {
  console.log("No properties");
}

to check if the brand object is empty with Object.keys(brand).length === 0.

Object.keys returns an array of non-inherited string keys in brand.

So if the returned array’s length is 0, then it’s empty.

Conclusion

To check if specific object is empty in TypeScript, we can use the Object.keys method.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.