Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 9056

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T06:50:08+00:00 2024-11-28T06:50:08+00:00

Interfaces in TypeScript

  • 60k

Abstract

An interface allows us to enforce a certain structure for an entity to follow. An interface contains the names and types of all the properties. It also includes the function signature, as well as the type of arguments and return type.

If a class implements an interface, it has to abide by it. That is, that class will have to implement all the properties and methods in that interface along with their types.

An interface acts as a contract with our application. It states what needs to be done but doesn't specify how it will be done. Further we cannot instantiate an interface. It can only be referenced by the class object that implements it.

Why Interfaces?

Interfaces helps maintain consistency in our code. It adds the functionality of strong type checking for your functions, variables, or the class that is implementing the interface. If an entity fails to properly implement an interface,the compiler will throw an error. This allows us to detect problems before they occur, which is definitely advantageous and simplifies maintenance in the long run.

Further Interfaces helps to improve the performance of JavaScript engines by making just one shape of the type of the interface and each of the objects just stores corresponding values of the properties as defined in the interface.

We should use an interface for the following:

  • Validating specific structure of properties
  • Objects as parameters
  • Objects returned from functions

Declaring Interfaces

We declare an interface using the interface keyword in a .ts file. The following example shows an example of the syntax:

interface Person {   name: string;   age: number;   speak(a: string): void;   spend(a: number): number; } 
Enter fullscreen mode Exit fullscreen mode

Here we've created an interface Person with properties name which is a string and age which is a number. Then, we have two methods namely speak which takes in a parameter of type string and returns void and spend which takes in a parameter of type number and returns number.

Now let us implement the interface:

let me: Person = {     name: 'shaun',     age: 30,     speak(language: string): void {             console.log(text);     },     spend(amount: number): number {             console.log('I spent ', amount);             return amount;     }, } 
Enter fullscreen mode Exit fullscreen mode

Here we have created an object me of type Person. Now, the compiler will expect the me object to have all the properties of the Person interface. If even one of the properties is missing or if its value isn't of the same type specified in the interface, the compiler will throw an error.

Specifying Optional Properties

In the previous code sample, all properties in the interface are required. There are certain cases, however, where we would expect our objects to have properties that are optional. We can achieve this by placing a question mark (?) just before the property's type annotation when declaring the interface:

interface Post {   title: string;   content: string;   tags?: string[]; } 
Enter fullscreen mode Exit fullscreen mode

In the above code, we are telling the compiler that it is possible to have Post objects without tags.

Specifying Read-only properties

A read-only property can only be changed when an object is created for the first time. That is, if you attempt to reassign its value, the compiler will generate an error.

We can mark a property as read-only by adding the readonly keyword before a property's name.

interface PlacedOrder {   itemsInOrder: number;   totalCost: number;   readonly dateCreated: Date; } 
Enter fullscreen mode Exit fullscreen mode

readonly vs const

The easiest way to remember whether to use readonly or const is to ask whether you're using it on a variable or a property. Variables use const whereas properties use readonly.

Interface As Functions

TypeScript interfaces can also be used to annotate functions. We can do so by creating a callable signature inside the interface.This is like a function declaration with only the parameter list and return type given.

interface SumFunc {   (a: number, b: number): number; }  const add: SumFunc = (num1, num2) => {   return num1 + num2; } 
Enter fullscreen mode Exit fullscreen mode

Apart from this, the function parameters can also be declared as the type of an interface.

Interface As Class Types

Interfaces can also be used to properly type classes created in JavaScript. To achieve this, we create an interface that contains the class' properties and methods, then we use the_implements_ keyword when creating our class.

interface Person {   name: string;   age: number; speak(a: string): void; spend(a: number): number; }  class Me implements Person{      name: string;     age: number;    speak(a: string): void;    spend(a: number): number;   private idNumber: number;     constructor(name: string, age: number) {        this.name = name;        this.age = age;    } } 
Enter fullscreen mode Exit fullscreen mode

Please note, the interface defines only the public properties of a class rather than both the public and private side.

Generic Interfaces

You may not always be sure of the type of data that each property in your interface contains. The simplest method would be to simply add any type to such properties. This, however, would make our code less type-safe.

To assist with scenarios like this, TypeScript allows you to create generic interfaces. Generic interfaces are defined in the same way as regular interfaces, but they allow you to provide one or more parameters in the interface's description which are further used to specify the type of a property inside the interface.

We can create a generic interface by passing type parameters, using angle brackets(<>), to our interface.

interface ApiData<T> {   payload: T[];   code: number;    date: Date; }  interface UserData {   name: string;   email: string; }  async function loadDataFromApi() {  const data: ApiData<UserData> = await fetch('/some/api/endpoint'') } 
Enter fullscreen mode Exit fullscreen mode

Here the “ApiData” converts to:

interface ApiData<T> {  payload: UserData[];  code: number;  date: Date; } 
Enter fullscreen mode Exit fullscreen mode

Extending an Interface

Just like in classes, the interface can also inherit from single or multiple interfaces as well as a class. To extend an interface, we use the extends keyword. We can extend multiple interfaces when they are separated by commas.

interface PetsInterface {   name: string;   } interface DogInterface extends PetsInterface {   breed: string; } interface CatInterface extends PetsInterface {   breed: string; } class Cat implements CatInterface {   name: string = 'Garfield';   breed: string = 'Tabby'; } class Dog implements DogInterface {   name: string = 'Rover';   breed: string = 'Poodle'; } 
Enter fullscreen mode Exit fullscreen mode

Conclusion

  • Interfaces defines the structure for entities to follow and they can be implemented by functions or classes. We can define optional properties on an interface using ? and read-only properties by using the readonly keyword in the property name.
  • Interfaces can be extended to import properties of other interfaces or classes using the extends keyword.
  • We can use generics with interfaces to create reusable components.
  • Interfaces also help in improving the performance of JavaScript engines.

References:

  • Ankita Masand's blog on logrocket
  • The net ninja's video on Typescript Interfaces

beginnersprogrammingtypescriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.