Object Interfaces

  • Object Interfaces are used to define the shape or structure of an object.
  • They define the properties with the types that an object should contain.
  • We use the ‘interface‘ keyword to declare an object interface followed by the interface name and its properties.
  • They can be implemented by other interfaces and classes, promoting code reusability and maintainability.

Example: This example shows the use of the TypeScript Interfaces.

Javascript




interface Employee {
    eid: string,
    name: string
}
const emp: Employee = {
    eid: "E54321",
    name: "Ravi"
};
  
console.log(emp);


Output:

{ eid: 'E54321', name: 'Ravi' }

TypeScript Object Interfaces vs. Intersections

TypeScript offers several features when it comes to defining complex data structures. In TypeScript, Object Interface and Intersections are two different features that serve different purposes when it comes to defining complex data structures.

Similar Reads

Object Interfaces

Object Interfaces are used to define the shape or structure of an object. They define the properties with the types that an object should contain. We use the ‘interface‘ keyword to declare an object interface followed by the interface name and its properties. They can be implemented by other interfaces and classes, promoting code reusability and maintainability....

Intersections

...

Object Interfaces vs. Intersections:

Intersections allow us to combine multiple types into a single type. We use the ‘&‘ operator to merge all properties and methods of each type involved. Intersections are used to compose multiple types by merging existing types or interfaces....