How To Check If An Object Implements An Interface In Typescript

Check if an Object implements an interface in TypeScript

To check if an object implements an interface in Typescript we will use the ‘in’ keyword. So how to do it? Let’s go into detail now.

Check if an object implements an interface in Typescript

Using the ‘in’ keyword

The ‘in’ operator will help you check if one property exists to specify the object in Typescript by returning a boolean value.

Example:

interface inforDetail {
    name: string,
    age: number,
    address: string
}
  
// Create object that implement inforDetail interface
const person: inforDetail = {
    name: "Jonnas",
    age: 25,
    address: "Finland",
};
  
// Check if the property exists in the object
console.log("name" in person);
console.log("age" in person);
console.log("address" in person);
console.log("phoneNumber" in person);

Output:

[LOG]: true 
[LOG]: true 
[LOG]: true 
[LOG]: false

Here I use the ‘in’ operator to check if the name, age, address, phoneNumber exist in the person object, which implements the inforDetail interface. As you see inforDetail interface doesn’t have the phoneNumber property, so it will return false. We can apply the ‘in’ operator to create a function that checks if an object implements an interface in Typescript by comparing all properties inside that object with an object that uses the interface.

Example:

interface inforDetail {
    name: string,
    age: number,
    address: string
}
  
// Function check if an object implements an interface in Typescript
const check = (obj: any): boolean => {
    return "name" in obj && "age" in obj && "address" in obj;
};
  
// Create object that implement inforDetail interface
const person: inforDetail = {
    name: "Jonnas",
    age: 25,
    address: "Finland",
};
  
// Create an object that doesn't implement the inforDetail interface
const person1: object = {
    name: "Tommy",
    age: 30,
};

console.log(check(person));
console.log(check(person1));

Output:

[LOG]: true 
[LOG]: false

Here I make a function check to check if an object implements an interface by comparing all properties inside inforDetail interface that exists in the object. I use logical AND (&&) to make check all properties. With AND logical, the true value only returns when all side is true.

Example about the function

So we can apply the check function to make a type guard if the input is valid, then we can execute our code logic.

Example:

interface inforDetail {
    name: string,
    age: number,
    address: string
}
  
// Function check if an object implements an interface in Typescript
const check = (obj: any): boolean => {
    return "name" in obj && "age" in obj && "address" in obj;
};
  
// Create object that implement inforDetail interface
const person: inforDetail = {
    name: "Jonnas",
    age: 25,
    address: "Finland",
};
  
// Function execute code
const logPerson = (obj: any) => {
    if (check(obj)) {
        console.log(
            `${obj.name} is ${obj.age} years old and lives at ${obj.address}`
        );
    }
};
  
logPerson(person);

Output:

[LOG]: "Jonnas is 25 years old and lives at
 Finland"

Summary

In this article, I have shown you how to check if an object implements an interface in Typescript. You can use the ‘in’ keyword to check if all properties in an interface exist in the object and infer based on it.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *