Creating interfaces
In this lesson, we learn what interfaces are and how to create them.
Understanding an interface #
An interface allows a new type to be created with a name and structure. The structure includes all the properties and methods that the type has without any implementation.
Interfaces don’t exist in JavaScript; they are only used by the TypeScript compiler type checking process.
Creating an interface #
We create an interface with the interface
keyword, followed by its name, followed by the properties and methods that make up the interface in curly brackets:
interface TypeName {
propertyName: PropertyType;
methodName: (paramName: ParamType) => MethodReturnType
}
As an exercise, create an interface called ButtonProps
that has a text
property of type string
and an onClick
method that ...