Typescript: Proposal: emit warning or error when coercing array to boolean

Created on 14 Mar 2017  ·  8Comments  ·  Source: microsoft/TypeScript

[email protected]

This snippet compiles without any warning:

const result: string[] = gimmeResult(); // returns an array type
if (!result) { 
 // code
}

And I'd argue it's almost always a masked bug (the programmer did not realise the return value was an array).

Would it be sensible to emit some sort of a warning?

Duplicate

Most helpful comment

At work:

image

...sorry couldn't help it, I'll stop spamming now :-)

All 8 comments

and if (result) { .. is not allowed either?

Duplicate #9041 ?

Or #14114

@mhegazy Yeah, good point. :-) I would say any array -> boolean coercion is a code smell and most probably a bug.

@RyanCavanaugh Thanks. Man, you must have a really good memory. :-D

I just keep all the issue numbers scrawled on a scrap of paper near my desk :wink:

@RyanCavanaugh the hyperspace In | n ∈ Z ≥ 14638 over the field of Typescript issues reduced to a single flat physical object 😲

@RyanCavanaugh Man, you must have a really large desk. :)

At work:

image

...sorry couldn't help it, I'll stop spamming now :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Zlatkovsky picture Zlatkovsky  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

siddjain picture siddjain  ·  3Comments

Antony-Jones picture Antony-Jones  ·  3Comments

MartynasZilinskas picture MartynasZilinskas  ·  3Comments