Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

TypeScript interfaces and their advantages in Ionic development

Ayyaz Sheikh

In TypeScript, we use interfaces to group and pass around data. They are used for type checking purposes.

We define an interface as having a property, define its data type, and when the function is called, it validates the data type of the property listed in the interface.

Note: The object that is passed to the function must contain that property (key) that is defined in the interface.

interface LabeledValue {
  label: string;
}
 
function printLabel(labeledObj: LabeledValue) {
  console.log(labeledObj.label);
}
 
let myObj = { size: 10, label: "Size 10 Object" };
printLabel(myObj);

In the above code snippet, we defined an interface LabeledValue that has one property label of type string. When the function PrintLabelis called, the interface definition will be invoked on myObj and the data type of label will be checked.

Here, it is worth noting that properties are not required to be in any order. What matters is the object having property(ies) and data type(s) that an interface requires.

RELATED TAGS

CONTRIBUTOR

Ayyaz Sheikh
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring