在 winston 中为信息方法添加缺少的签名

Add missing signature for info method in winston

我正在使用打字稿中的 winston 创建自定义记录器。 info 方法应该处理作为对象的单个参数。不幸的是,definitelyTyped 中 winston 的类型没有为 info 提供这个签名。所以我尝试扩展接口以添加缺少的类型。但是没有考虑进去。

我在编译时遇到这个错误:

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

第 23 行是 logger.info(o) 行。

我的源文件:

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;
  }
}

编辑解决方案:

expandWinston.d.ts中的最终代码:

import { LoggerInstance } from "winston";

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

你确定 winston 支持吗?
从他们的 github 页面上看起来不像,但我没仔细看。

无论如何,您可以像这样更新编译器:

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