1

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.

  • 1
    Possible duplicate of [All falsey values in JavaScript](https://stackoverflow.com/questions/19839952/all-falsey-values-in-javascript) – Søren D. Ptæus Jun 11 '18 at 12:00
  • 3
    Note: TypeScript is a superset of JavaScript. In JavaScript – and therefore in TypeScript – **only certain values evaluate to** `false`. Everything else evaluates to `true`. See the section [**Some more truthy values** in the accepted answer](https://stackoverflow.com/a/19839953/5389131) of the duplicate I linked to. It specifically mentions **objects** and **functions** such as `function(){}` or `() => {}` evaluate to `true`. – Søren D. Ptæus Jun 11 '18 at 12:04
  • To add to what Søren said, consider: `function f() { return false; } f = undefined;` –  Jun 11 '18 at 12:08
  • @AndyJ actually the compiler will not let you do that with function declarations. – Titian Cernicova-Dragomir Jun 11 '18 at 12:09
  • @TitianCernicova-Dragomir And if `f = undefined;` happened in a JavaScript file outside of the compilers knowledge? The compiler can't know whats happening at runtime, as it only operates on whats available at compile time. –  Jun 11 '18 at 12:11
  • You can change your coding style to help avoid these types of errors. Using `===` will do a "strict" equality check, which will also test the types of the two values being compared. `if( f === true )` will cause a type mismatch error "Operator '===' cannot be applied to types '() => boolean' and 'boolean'". –  Jun 11 '18 at 12:24
  • @AndyJ This is actually what I was looking for. This solution can detect such errors for me long before I compile/fire up my code, thanks a lot. I will edit the post in a minute. –  Jun 11 '18 at 12:38

1 Answers1

0

is there any real-world reason for evaluation function object to true?

It allows you to test if they exist.

For example, a JavaScript library might have plugins which optionally allow for functions to be implemented. It can then test if a plugin implements the function, and if it does then it uses it.

As to whether that is good practice is outside the scope of this answer, but JavaScript allows it, so TypeScript supports it.

There is a way to modify your coding style to detect this type of error though.

Using === will do a "strict" equality check, which will also test the types of the two values being compared. if( f === true ) will cause a type mismatch error "Operator '===' cannot be applied to types '() => boolean' and 'boolean'".