3

I want to create a wrapper in TypeScript around console.log like this:

var mylog = (...args: any[]) => { console.log(args); };

So I loglike this:

if (!!mylog) mylog('text', variableA, variableB);

And can set it to null to disable logging.

Problem is: this doesn't work!

2
  • Wouldn't it be simpler to not do if(!!myLog) and instead set it to an empty function if you want to disable logging? Commented Sep 14, 2014 at 10:29
  • @Benjamin: you are right! Much simpler... Commented Sep 14, 2014 at 18:32

1 Answer 1

9

It works fine:

var mylog = (...args: any[]) => { console.log(args); };

if (!!mylog) mylog('text', 1, 2); // ['text',1,2]

mylog = null; 

if (!!mylog) mylog('text', 1, 2); // nothing printed

Perhaps you don't want it to print as an array. You can just use apply:

var mylog = (...args: any[]) => { console.log.apply(console,arguments); };
Sign up to request clarification or add additional context in comments.

1 Comment

You can also simplify apply with spread syntax: var mylog = (...args: any[]) => { console.log(...args) }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.