Overview of Interfaces in TypeScript

Let's learn about using an interface to ensure strongly typed code.

What is an interface?

An interface is an abstract type in TypeScript that informs the compiler about the structure of an object. We can utilize interfaces to build custom data types, allowing us to strongly type our code.

Let’s consider the following example to better understand the concept of interface. We’ve defined an interface called User, which has two properties—firstName and lastName. These two properties are both of type string.

We also declared a variable person and assigned it the data type User. As a result, the person object must implement the User structure. To view the value of person, click the “Run” button below.

Get hands-on with 1200+ tech skills courses.