Search code examples
expresstypescripthybrid

Typescript overwrite express send function with hybrid interface


I'm trying to overwrite the node express send method of the Response type. The method is of type Send that is defined as such:

interface Send {
    (status: number, body?: any): Response;
    (body?: any): Response;
}

My objective is to add local logging of the response send with express, but I can't make an implementation of this type, even as a function like explained in other questions like this.


Solution

  • That interface describes a function with two signatures, you can implement it like so:

    const fn: Send = (first?: any, second?: any) => {
        if (first && second) {
            // deal with (status: number, body: any)
        } else if (first) {
            // deal with (status: number) or (body: any)
        } else {
            // deal with ()
        }
    
        return new Response();
    }