lundi 11 juin 2018

Why do functions themselves evaluate to true in TypeScript?

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?

Aucun commentaire:

Enregistrer un commentaire