Interfaces
Interfaces are very similar to types and sometimes developers don’t know the differences between them. Interfaces can be used to describe the shape of an object or function signature just like types, but the syntax is different:
interface User {
username: string
email: string
name: string
age?: number
website: string
active: boolean
}
You can name an interface as you want, but a good practice to follow is to add a prefix of I
. For example, the User
interface will become IUser
. In this way, you can quickly recognize that it is an interface, and you don’t get confused thinking it is a class or a React component.
An interface can also be extended, implemented, and merged.