TypeScript: true == false - cannot be applied to types 'false' and 'true'.

TypeScript Version: Version 2.1.0-dev.20160927

Code

const a = false;
const b = a === true; //  Operator '===' cannot be applied to types 'false' and 'true'.
const c = true === false; // that is ok

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 17 (7 by maintainers)

Most upvoted comments

@jasonswearingen see #9998 and the many bugs linked from it

Code flow analysis is doing a better tracking of code than you might assumeā€¦ In this case CFA has determined the only value b might have at this point in the code is false.

The problem goes away, when the value cannot be statically determined anymore:

var b: boolean = false;

b = Math.random() % 2 ? true : false;

b === true ? 'n_' : '';

It is the goal of TypeScript to statically identify constructs that are likely to be errors, which a variable, without reassignment, within the same closure cannot be any other value than false so it is invalid to compare it to true.