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.
We'll cover the following...
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.
Be careful; an interface is not an object, so we use ; and not ,.
We can also set optional properties like this:
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 ...