Categories
TypeScript Answers

How to define TypeScript callback types?

Spread the love

Sometimes, we want to define TypeScript callback types.

In this article, we’ll look at how to define TypeScript callback types.

How to define TypeScript callback types?

To define TypeScript callback types, we can set the callback to have a function with the signature and return type.

For instance, we write

class C {
  public myCallback: () => void;

  public doWork(): void {
    this.myCallback();
  }
}

to set the myCallback method’s type to () => void.

() means myCallback has an empty signature.

And void myCallback returns nothing.

Conclusion

To define TypeScript callback types, we can set the callback to have a function with the signature and return type.

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 *