为s3-streamlogger编写TypeScript定义

时间:2018-06-02 10:48:02

标签: node.js typescript typescript-typings winston

我试图为s3-streamlogger编写一个TypeScript定义文件,但是我已经卡住了。这是我目前在s3-streamlogger.d.ts中所拥有的内容:

/// <reference types="node" />

declare module "s3-streamlogger" {
    import { Writable } from "stream";

    export interface S3StreamLogger extends Writable {
        new (options: {}) : S3StreamLogger;
    }
}

这个几乎似乎有效 - 我尝试使用s3-streamlogger作为Winston传输,而Winston似乎很满意它,但是当我尝试设置一个新的s3streamlogger时,我得到了:'S3StreamLogger' only refers to a type, but is being used as a value here.

s3stream = new S3StreamLogger({
        access_key_id: process.env.S3_ACCESS_KEY_ID,
        bucket: process.env.S3_BUCKET,
        folder: process.env.NODE_ENV,
        max_file_size: 50 * 1024 * 1024,
        name_format: `%Y/%m/%d/%Y-%m-%dT%H:%M:000000Z.log`,
        rotate_every: 3600 * 1000,
        secret_access_key: process.env.S3_ACCESS_KEY_SECRET,
        upload_every: 1000,
    });

我很确定这与我导出界面这一事实有关,但是看看Winston类型,它们似乎也导出了Console传输的接口:

interface ConsoleTransportInstance extends Transport {
    name: string;
    stderrLevels: string[];
    eol: string;

    new(options?: ConsoleTransportOptions): ConsoleTransportInstance;
}

我可以&#34;新&#34;这很好:

logTransports.push(
    new transports.Console({level: "error"}),
);

我怀疑它是相关的,因此底层的NodeJS s3-streamlogger被定义为一个新的函数:

function S3StreamLogger(options){
...
}

module.exports = {
    S3StreamLogger: S3StreamLogger
};

为这样的新功能编写TypeScript定义的正确方法是什么?

谢谢!

0 个答案:

没有答案