简体   繁体   中英

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. I am using an existing library (ngx-logger) that I can't/want to modify. I am trying to create a service that returns a logger object configured to a specific logging level:

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. The problem is that if I extend the interface, the line "logger.updateConfig(config)" would not work because the type is different, right?

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. Hope this makes more sense

export interface CustomConfig extends ObjectConfig {
   name: string;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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