简体   繁体   English

如何将自定义属性添加到 typescript 接口

[英]How to add a custom property to a typescript interface

EDITED THE EXPLANATION:编辑说明:

First, I am new to typescript so I am sorry if this a very basic question.首先,我是 typescript 的新手,如果这是一个非常基本的问题,我很抱歉。 I am using an existing library (ngx-logger) that I can't/want to modify.我正在使用我不能/不想修改的现有库 (ngx-logger)。 I am trying to create a service that returns a logger object configured to a specific logging level:我正在尝试创建一个返回配置为特定日志记录级别的记录器 object 的服务:

public getLogger(name: string): NGXLogger {
    
    // if the logger is defined, then create a new one and update its config
    if (name in this.logCfg['loggers']) {
        // using a deep copy so only this logger is affected
        let logger = _.cloneDeep(this.rootLogger);
        let level = this.getLevel(this.logCfg['loggers'][name]['level']);
        let config = this.rootLogger.getConfigSnapshot();
        config.level = level;
        // THIS IS WHAT I AM TRYING TO DO
        config.name = name;
        logger.updateConfig(config);
        return logger;
    }
    // was not found so use root logger
    return this.rootLogger;
}

I would like to be able to add the name of the logger to the config object which is an interface.我希望能够将记录器的名称添加到接口配置 object 中。 The problem is that if I extend the interface, the line "logger.updateConfig(config)" would not work because the type is different, right?问题是,如果我扩展接口,“logger.updateConfig(config)”这一行将不起作用,因为类型不同,对吧?

The reason why I want to store the name of the logger is to be able to add that information to the metadata being printed every time the methods debug|info|warn|error are called.我想要存储记录器名称的原因是能够在每次调用方法 debug|info|warn|error 时将该信息添加到正在打印的元数据中。 Hope this makes more sense希望这更有意义

export interface CustomConfig extends ObjectConfig {
   name: string;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM