Interfaces

So far, we have been looking at several new additions to JavaScript that have been made available to us. This section on interfaces, for our purposes, is a TypeScript-specific thing, since JavaScript does not have the notion of interfaces.

Interfaces are like a contract for a class and provide a set of rules that the class must follow. Let's switch gears from building a Car class to building an Animal class, and, while we're at it, let's have our class implement an interface we'll call Species

class Animal implements Species {
}
interface Species {
name: string;
isExtinct: boolean;
}

Our Animal class is empty. It doesn't even have a constructor or any instance variables, and that is not a problem for us, since it still serves our purposes to demonstrate how to use interfaces.

Take a look at the Species interface for a moment. You'll notice a couple of things:

  • It has two public properties. TypeScript has access modifiers, just like Java and C# have, and we'll get to those when we make use of them in later chapters. For now, all you need to know is that the lack of an access modifier on the properties makes the properties public. This is important because since an interface describes the public interface of the class that implements it, its properties must be public.
  • The second thing that you'll notice is that we're typing the properties. We're declaring the name property as being of type string, and the isExtinct property as being of type boolean. This is one major advantage of TypeScript, as we've previously learned, and is where TypeScript got its name (that is, a typed JavaScript).

We'll see access modifiers in action later in the book. There are three of them:

  • Public: This is the default modifier and it means that the property or function is visible to all other code
  • Private: The visibility to a class's properties and functions marked as private are only available to member functions of the class that they're declared in
  • Protected: This is the same as private, but the class members are also visible to any classes that are inherited from the class that they're declared in

The way in which we marry the class to the interface is by using the implements keyword in the class definition, as we have done so in this example. Once we do that, the class must adhere to the interface contract. 

So now what? Well, if the Animal class doesn't implement the two properties that the Species interface says it must, then TypeScript will throw an error during transpilation.

We can also have an interface describe an optional contract, and we can do this by appending a question mark to the end of the property or function. We don't have a function listed in our interface, but we can absolutely have functions as well. 

If our interface was an optional contract, it would look like this:

interface Species {
name?: string;
isExtinct?: boolean;
}
..................Content has been hidden....................

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