typescriptmodulebunyan

Exporting a type from another module with all type info


I have a module that contains one export. It's a factory function to create a bunyan logger:

export default function createLogger(options: LoggerOptions, /*...*/): Logger {
    // returns a bunyan Logger with some extra stuff for our company
}

I want to use this module in other applications, and I want to include the type info of the bunyan Logger instance, so that we have intellisense for stuff like logger.info, logger.warn, etc.

So, I exported both my factory function and the bunyan Logger from my index.ts file:

import Logger from "bunyan";
import createLogger from "./createLogger";

export {
    createLogger,
    Logger
};

However, when I import this in another application, I can see that my IDE thinks logger is of type any:

type is any

Is this because bunyan doesn't export the Logger type? Or because in my application, I only installed my custom package and not bunyan? I would prefer that users of the custom package don't have to install both my package and bunyan.

Is there a way to fix this, ie have intellisense of the bunyan logger without having to separately install bunyan?


Solution

  • I found the solution myself. As bunyan exports a namespace, I had to change my index.ts to:

    import * as Logger from "bunyan";
    import createLogger from "./createLogger";
    
    export {
        createLogger,
        Logger
    };
    

    Notice how I do import * as Logger from "bunyan", which imports everything from bunyan and calls it "Logger". Now everything works as expected.