How to add optional function in a TypeScript interface?

Sometimes, we want to add optional function in a TypeScript interface.

In this article, we’ll look at how to add optional function in a TypeScript interface.

How to add optional function in a TypeScript interface?

To add optional function in a TypeScript interface, we add a ? after the function name.

For instance, we write

interface IElement {
  name: string;
  options: any;
  type: string;
  value?: string;
  validation?(any): boolean;
}

to make the validation function optional by putting a ? after the function name.

Conclusion

To add optional function in a TypeScript interface, we add a ? after the function name.