I recently started learning TypeScript. I am wondering why do following construction work with no errors/warnings in this language:
function f(): boolean {
return false;
}
if ( f ) {
performSomeAction();
}
(and will actually run conditional part of the code), while most of the programmers would probably admit it's a typo (lack of call parenthesis of function) and rewrite it this way:
if( f() ){
performSomeAction();
}
I mean, is there any real-world reason for evaluation function object to true? Or rather it's simply something that makes the code more vulnerable to such errors?
[SOLUTION] To avoid this kind of errors, one can change the coding style. In conditional statement one can use "===" strict egality check, which in the first example will result in "Operator '===' cannot be applied to types '() => boolean' and 'boolean'" TypeScript compiler error.