How to add an optional function in interface with TypeScript?

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.