
Learning Angular
By :

An interface is a code contract that defines a particular schema. Any artifacts like classes and functions implementing an interface should comply with this schema. Interfaces are beneficial when we want to enforce strict typing on classes generated by factories or when we define function signatures to ensure that a particular typed property is found in the payload.
Interfaces disappear during transpilation and are not included in the final JavaScript code.
In the following snippet, we define an interface for managing products:
interface Product {
name: string;
price: number;
getCategories: () => string[];
}
An interface can contain properties and methods. In the preceding snippet, the Product
interface contains name
and price
properties. It also defines the getCategories
method. A class can use an interface by adding the implements
keyword and the interface name in the class declaration:
class Keyboard implements Product {
name: string = 'Keyboard&apos...