Get the length of the object in typescript

To get the length of the object in typescript, you can use the Object.keys() method with the length property it will return the length of the object.

// ✅ Get the length of the object in typescript
let num: number = 493054;
Object.keys({id: 1, name: "infinitbility.com"}).length;
// 👉️ 2

Today, I’m going to show you How do I get the length of the object in typescript, as above mentioned here, I’m going to use theObject.keys() method with the length property to get the length of objects;

Let’s start today’s tutorial how do you get the length of the object in typescript?

First, I will take the sample object variable with sample data, then I will use Object.keys() to get an array of keys and use the length property, let’s console it.

// ✅ Get the length of the object in typescript

interface domain {
    id: number,
    name: string
}

let obj: domain = {id: 1, name: "infinitbility.com"};
let lenOfObj: number = Object.keys(obj).length;
console.log(lenOfObj)
// 👉️ 2

I hope it helps you, All the best 👍.