Open In App

How to Check if all Enum Values Exist in an Object in TypeScript ?

Last Updated : 16 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

To check if all values of an enum exist in an object in TypeScript, iterate through the enum values and verify their presence in the object. This ensures that the object encompasses all possible enum values, confirming completeness and adherence to the enum definition. There are various methods to check if all the enum values exist in an object which are as follows:

Using Object.keys() and includes()

In this approach, To check if all enum values exist in an object, use TypeScript’s Object.keys() and includes(). Iterate through enum keys, then verify their presence in the object. This ensures the object incorporates all enum values.

Syntax:

const allEnumValuesExist = Object.keys(EnumType).every(key =>
Object.values(yourObject).includes(EnumType[key])
);

Example: In this example, checks if all values of the Branch enum exist in the department object using Object.keys() and includes(). In this case, it returns true since the only enum value matches the department’s branch.

Javascript
enum Branch {
    CivilEngineering = "Civil Engineering",

}

const department = {
    branch: "Civil Engineering",
    manager: "Mukesh",
    location: "Building A",
};
const allEnumValuesExist = 
    Object.keys(Branch).every(key => 
    {
         return Object.values(department).includes(Branch[key])
    });

console.log(allEnumValuesExist); 

Output:

true

Using Object.values() and every()

In this approach, TypeScript utilizes Object.values() and every() to confirm if all values of an enum, such as Branch, are present in an object, e.g., department. This concise method iterates through enum values, checking their inclusion in the object, ensuring completeness and adherence to predefined enum values.

Syntax:

const allEnumValuesExist = Object.values(EnumType).every(value =>
Object.values(yourObject).includes(value)
);

Example: This TypeScript example checks if all values of the Branch enum exist in the department object using Object.values() and every(). It outputs true as the object incorporates all defined enum values.

Javascript
enum Branch {
    CivilEngineering = "Civil Engineering",
    MechanicalEngineering = "Mechanical Engineering",
    ElectricalEngineering = "Electrical Engineering",
}

const department = {
    branch: "Civil Engineering",
    manager: "Nikita",
    location: "Building A",
};

// Check if all enum values exist in the object
const allEnumValuesExist = 
    Object.values(Branch).every(value => 
{
  return Object.values(department).includes(value)
});


console.log(allEnumValuesExist);

Output:

false

Using Object.entries() and every()

In this method, TypeScript’s Object.entries() and every() functions are employed to ensure that all values of an enum, such as Branch, are present in an object, e.g., department. This technique iterates through enum key-value pairs, validating their existence in the object, thereby confirming adherence to the predefined enum values.

Syntax:

const allEnumValuesExist = Object.entries(EnumType).every(([key, value]) =>
Object.values(yourObject).includes(value)
);

Example: This TypeScript example verifies if all values of the Branch enum exist in the department object using Object.entries() and every(). It returns true as the object contains all defined enum values.

JavaScript
enum Branch {
    CivilEngineering = "Civil Engineering",
    MechanicalEngineering = "Mechanical Engineering",
    ElectricalEngineering = "Electrical Engineering",
}
 
const department = {
    branch: "Civil Engineering",
    manager: "Nikita",
    location: "Building A",
};
 
// Check if all enum values exist in the object
const allEnumValuesExist = 
    Object.entries(Branch).every(([key, value]) => 
{
  return Object.values(department).includes(value)
});
console.log(allEnumValuesExist);

Output:

false


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads