To begin, create a new Interface
If you are working with TypeScript & Angular CLI, you can generate an interface using the following command
ng g interface car
Next, define the data types for its properties
// car.interface.ts
export interface car {
id: number;
eco: boolean;
wheels: number;
name: string;
}
You can now import your interface into the desired class.
import {car} from "app/interfaces/car.interface";
Then, update the collection/array of car objects by adding items to the array.
this.car.push({
id: 12345,
eco: true,
wheels: 4,
name: 'Tesla Model S',
});
Further information on interfaces:
An interface is specific to TypeScript and not part of ECMAScript. It acts as a contract defining the function's expected arguments and their types. In addition to functions, interfaces can also be applied to Classes to establish custom types.
Interfaces are abstract types that do not contain any code like classes. They solely outline the 'signature' or structure of an API. When transpiled, an interface doesn't produce any code; it's used by Typescript for type validation during development. -