The course is part of this learning path
This module introduces you to Interfaces in TypeScript.
The objectives of this module are to provide you with an understanding of:
- How to use Function Types
- How to use Optionals
- How to use Excess Property Checks
- How to use Indexable Types
- How Interfaces work with Classes
- How to create Extendable Interfaces
This learning path is aimed at all who wish to learn how to use TypeScript
We welcome all feedback and suggestions - please contact us at firstname.lastname@example.org to let us know what you think.
Ensuring our class meets a particular contract is one of the most common uses of interfaces in C# and Java. It's also possible in TypeScript. We can define both properties and methods in the interface, and a class that signs the contract must implement them. The code shown applies the Vehicle interface to the car class. This is done using the implements keyword and the name of the interface. And if we wanted to use several interfaces, we would separate their names with a comma. A class implements the interface by providing a way to set its fluxCapacitor property if it's present. And an implementation for the required accelerate method. It also has a method to determine if time travel is possible. We instantiate the class, and then use the accelerate method to increase the speed. The new speed will be logged out as 500. We also call the timeTravel function, passing in the instance's value of fluxCapacitor as the argument. This would output Time travel not possible, as the car was not instantiated with a flux capacitor.
About the Author