Categories
TypeScript Answers

How to add an optional function in interface with TypeScript?

Spread the love

Sometimes, we want to add an optional function in Interface with TypeScript.

In this article, we’ll look at how to add an optional function in Interface with TypeScript.

How to add an optional function in Interface with TypeScript?

To add an optional function in Interface with TypeScript, we can add ? after the function name.

For instance, we write

interface I {
  validation?(flag: any): boolean;
}

to create the I interface that has the validation function.

We make the function by add ? after validation.

Conclusion

To add an optional function in Interface with TypeScript, we can add ? after the function name.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *