Interface

TypeScript has a special keyword called interface that doesn't exist in ECMAScript. Interfaces fill the role of naming the types in TypeScript and is used to describe the contracts within the code:

interface ICar {
make: string;
model: string;
year: number;
vin?: string;
}

Here, we have an interface for Car called ICar; we have added I before Car to describe it as an interface. In our ICar interface, we have make and model as string types, and year as a number. We also have an optional vin in our object shape with the type of string, it is optional is represented by ?.

To use this interface, we assign the ICar interface to the car variable, as follows:

const car: ICar = {
make: 'Chevrolet',
model: 'Malibu',
year: 2019,
};

This is how TypeScript helps you to assign different types and also use interfaces to define types of an object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.144.32