How to define TypeScript callback types?

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.