Typescript: error TS1345: An expression of type 'void' cannot be tested for truthiness - console.log(x) || x

Created on 31 Oct 2018  ·  3Comments  ·  Source: microsoft/TypeScript

In a functional context, I often use loggers like this;

const logger = (...args) => console.log(args) || args;

As console.log() returns undefined, it will always fall through the next argument. However, my angular project now no longer compiles.

-->


TypeScript Version: 3.1.4


Search Terms:
error TS1345: An expression of type 'void' cannot be tested for truthiness
found some issues, but not the same.
Code

const logger = (...args) => console.log(args) || args;

Expected behavior:
Compile

Actual behavior:
gives error

Most helpful comment

Change it to use comma operator:

const logger = (...args) => (console.log(args), args);

See the discussion about this change: #26262.

All 3 comments

Change it to use comma operator:

const logger = (...args) => (console.log(args), args);

See the discussion about this change: #26262.

@RyanCavanaugh or @DanielRosenwasser, probably you should add Use of 'void' in control flow constructs is now disallowed to the breaking changes wiki page.

closed as duplicate

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MartynasZilinskas picture MartynasZilinskas  ·  3Comments

jbondc picture jbondc  ·  3Comments

wmaurer picture wmaurer  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

uber5001 picture uber5001  ·  3Comments