DEV Community

Antonin J. (they/them)
Antonin J. (they/them)

Posted on • Edited on

4 2

How To Describe A Function Typing Using An Interface In TypeScript

I keep forgetting how to do this but TypeScript supports describing a function signature (its arguments and return) using an interface and it's not that difficult. For example, to describe a regular Node-style callback, you might write something like this:

interface NodeStyleCallback {
  (err, data): any
}
Enter fullscreen mode Exit fullscreen mode

The syntax is essentially a regular interface as you're used to and arguments in the parentheses with a return type:

interface SomeFunction {
  (arg1, arg2, arg3, arg4): any
}

function someFunction(arg1, arg2, arg3, arg4) {
 // doesn't matter what the return, it'll always satisfy `any`
}
Enter fullscreen mode Exit fullscreen mode

You can also assign types to the arguments!

interface Sum {
  (arg1: number, arg2: number): number
}
Enter fullscreen mode Exit fullscreen mode

And while you can't assign an interface to a function, you can use the interface to describe variables -- include arguments like so:

interface IObject {
  [key: string]: any
}

interface EventHandler {
  (e: IObject): any
}

const handler: EventHandler = (e) => {
  console.log(e);
};

function handleOnClick(domEl, eventHandler: EventHandler) {
  domEl.addEventListener('click', eventHandler);
}
Enter fullscreen mode Exit fullscreen mode

What about generics?

You can make function interfaces generic just like any other interface.

interface SpecialHandler<U> {
  (event: U): any
}

const handler: SpecialHandler<string> = (eventString) => {
  document.title = eventString;
};

function registerEvent(eventName, handler: SpecialHandler) {

}
Enter fullscreen mode Exit fullscreen mode

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (4)

Collapse
 
cubiclebuddha profile image
Cubicle Buddha

You can also add JsDoc style comments (via /** style comments) in the interface so the consumer gets that documentation in the IntelliSense output. Great stuff! I enjoyed your article.

Collapse
 
antjanus profile image
Antonin J. (they/them)

NICE! I actually didn't know that!

Collapse
 
cubiclebuddha profile image
Cubicle Buddha

Great. Yea, as you can probably tell by the wacky titles of my articles (“Communicating Your Needs: TypeScript’s Value From A Buddhist Perspective”) I’m very passionate about using TypeScript to document the code in easy, meaningful ways. :)

Thread Thread
 
antjanus profile image
Antonin J. (they/them)

I'll go read it!

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay