pinojs / pino-http

🌲 high-speed HTTP logger for Node.js
MIT License
539 stars 117 forks source link

TypeScript error when using customLevels on pino parent instance #317

Closed GeoffreyEmerson closed 10 months ago

GeoffreyEmerson commented 10 months ago

Using pino@8.17.1 and pino-http@8.6.0 :

import pino from "pino";
import pinoHttp from "pino-http";

const log = pino({
  customLevels: {
    metric: 25
  }
});

export const logRequestsMiddleware = pinoHttp({
  logger: log
});

Build error:

create-logger.ts:11:3 - error TS2769: No overload matches this call.
  Overload 1 of 2, '(opts?: Options<IncomingMessage, ServerResponse<IncomingMessage>> | undefined, stream?: DestinationStream | undefined): HttpLogger<...>', gave the following error.
    Type 'Logger<"metric">' is not assignable to type 'Logger<never> | undefined'.
      Type 'Logger<"metric">' is not assignable to type 'Logger<never>'.
        Type 'Logger<"metric">' is not assignable to type 'LoggerExtras<never>'.
          Types of property 'onChild' are incompatible.
            Type 'OnChildCallback<"metric">' is not assignable to type 'OnChildCallback<never>'.
              Types of parameters 'child' and 'child' are incompatible.
                Type 'Logger<never>' is not assignable to type 'Logger<"metric">'.
                  Type 'Logger<never>' is not assignable to type 'LoggerExtras<"metric">'.
                    Types of property 'customLevels' are incompatible.
                      Property 'metric' is missing in type '{}' but required in type '{ metric: number; }'.
  Overload 2 of 2, '(stream?: DestinationStream | undefined): HttpLogger<IncomingMessage, ServerResponse<IncomingMessage>, never>', gave the following error.
    Object literal may only specify known properties, and 'logger' does not exist in type 'DestinationStream'.

11   logger: log
iago-silva-unvoid commented 10 months ago

Same here.

Hot fix I am using:

import pino from "pino";
import pinoHttp from "pino-http";

const log = pino<never>({
  customLevels: {
    metric: 25
  }
});

export const logRequestsMiddleware = pinoHttp({
  logger: log
});

Notice the never generic to comply with pinoHttp function.