0

I am trying to create an instance of this interface

interface Logger {
  (...args: any[]): void;
  error(...args: any[]): void;
  warn(...args: any[]): void;
  info(...args: any[]): void;
  verbose(...args: any[]): void;
}

i.e it has to be both callable and it has to be possible to access the properties.

const log: Logger = ...implementation //this is what I am looking for
log('asd') // this should work
log.error('foo') // this should work

How do I achieve this behaviour?

Flux
  • 410
  • 1
  • 5
  • 19

1 Answers1

0

Solution is the same as for this question. Credit to @SamaraSoucy-MSFT

You implement it by creating a closure that invokes immediately. Define the main method and add properties to it. Example below.

const log = (function () {
  let main = function () { console.log(...arguments); }
  main.info = function () { console.log(...arguments); };
  main.error = function () { console.log(...arguments); };
  //...other methods

  return main;

})();


log('asd')
log.info('this is info')
log.error('this is error')
Flux
  • 410
  • 1
  • 5
  • 19