Search⌘ K
AI Features

Interfaces, Classes and More

Understand the fundamentals of TypeScript by exploring interfaces to define object shapes, classes for inheritance with access modifiers, and advanced types like intersections and unions. This lesson equips you to structure complex types and enhance code reusability effectively.

Interfaces #

In one of the prior examples I set the type of our variable like this: Array<{label:string,value:string}>

But what if the shape of our variable is much more complicated and we need to reuse it in multiple places? We can use an interface to define the shape that that variable should have.

TypeScript 3.3.4
interface Car {
wheels: number;
color: string;
brand: string;
}

Be careful; an interface is not an object, so we use ; and not ,.

We can also set optional properties like this:

TypeScript 3.3.4
interface Car {
wheels: number;
color: string;
brand: string;
coupe?: boolean;
}

The ? defines the property as optional, even if we create a new car object without it, TypeScript won’t raise any error.

Maybe we don’t want certain properties to be editable after the creation of the object, in that case ...