Search code examples
typescriptwinstondefinitelytyped

Add missing signature for info method in winston


I am creating a custom logger using winston in typescript. The info method is supposed to handle a single argument which is an object. Unfortunately, the types for winston in definitelyTyped don't provide this signature for info. So I tried to extend the interface to add the missing type. But it is not taken into account.

I get this error while compiling:

example.ts(23,13): error TS2345: Argument of type '{ bar: string; demo: string; }' is not assignable to parameter of type 'string'.

The line 23 is the logger.info(o) line.

My source files:

example.ts:

/// <reference path="./expandWinston.d.ts"/>

import * as winston from "winston";

const logger = new winston.Logger({
  transports: [
    new (winston.transports.Console)({
      colorize: true,
      handleExceptions: true,
      humanReadableUnhandledException: true,
      json: false,
      prettyPrint: true,
      timestamp: true,
    }),
  ],
});

const o = {
  bar: "baz",
  demo: "foo",
};

logger.info(o);

expandWinston.d.ts:

declare namespace winston {
  export interface LoggerInstance {
    info(meta: any): LoggerInstance;
  }
}

EDIT with solution:

Final code in expandWinston.d.ts:

import { LoggerInstance } from "winston";

declare module "winston" {
  interface LeveledLogMethod {
    (meta: any): LoggerInstance;
  }
}

Solution

  • Are you sure that winston supports that?
    Doesn't look like it from their github page, but I haven't looked that hard.

    In any case, you can update the compiler like this:

    declare module "winston" {
        interface LeveledLogMethod {
            (meta: any): LoggerInstance;
        }
    }