Interfaces, Classes and More
Learn how to create interfaces and classes in this lesson.
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.
Press + to interact
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:
Press + to interact
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 ...