Hello Friends đź‘‹,

Welcome To Infinitbility! ❤️

This tutorial will help you to check objects are empty or not in typescript, here we are using the Object.keys() method to check object has value or not.

As we know, to check an empty array we are using array length to verify array is empty or not in an object there is .length property not available then we have to first create an object to array for length using Object.keys() method.

Let start today’s tutorial How to check the empty object in typescript?

The Object.keys() method creates a separate array of object keys and now we are able to check their length to verify object is empty or not.

Let create an empty object in typescript. here I’m creating a new object but you can also use the class object.

let obj: object = {};

Now when we try to check their length obj.length, we will get undefined.

let create object keys to array and check the length.

Object.keys(obj).length;

Well, when we get length property. let use in if and else to check object empty or not.

let obj: object = {};

console.log(obj.length); // undefined

let objLength = Object.keys(obj).length;  // 0

if(objLength == 0){
  console.log("Empty Object");
}

Output

Empty, Object, typescript, Example