TypeScript throws error even after declared types are found in @types/morgan

42 views Asked by At

I am developing an express application in NodeJS and using the morgan middleware for logging. I am writing the code using typescript and have installed the required types for express, morgan and node. Whenever I try to compile the code to commonjs file, it throws the following error:

Argument of type 'string' is not assignable to parameter of type 'FormatFn<IncomingMessage, ServerResponse<IncomingMessage>>'

The code for my morgan middle ware setup is like:

app.use("*", morgan(":remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms", {
    skip: function (req: Request, res: Response) { return res.status < 400 }
}));

I tried removing the options object, it works fine then but I need to filter some logs from my console otherwise it will be flooded. On my manual inspection of @types/morgan, I found the relevant declaration of

declare function morgan<
    Request extends http.IncomingMessage = http.IncomingMessage,
    Response extends http.ServerResponse = http.ServerResponse,
>(
    format: string,
    options?: morgan.Options<Request, Response>,
): Handler<Request, Response>;

But this is not being considered while compilation for some reason, I guess.

0

There are 0 answers