Search⌘ K

Interfaces

Explore how TypeScript interfaces provide a contract for the expected structure of objects in Ionic development. Understand their role in defining properties and data types to prevent errors when parsing JSON data with Angular's HttpClient. Gain insight into best practices for asserting data types to improve code reliability.

Although not technically required for application development, we want to introduce TypeScript interfaces and spend a little time exploring them. The reason for this will become clear shortly.

What is an interface?

An interface is a programming feature that helps define an object’s expected structure, serving as a contract of sorts for the class where it’s used.

Let’s say that we have the following method:

TypeScript 3.3.4
public requestSpecificArea(coords : any) : Observable {
return this.http.get(this._api + `locate-region?q=${coords}`);
}

It uses Angular’s HttpClient get method to return JSON data structure, like ...