This project is read-only.

&& return type is too specific

Topics: Language Specification
Oct 1, 2012 at 7:01 PM

&& needs to be like || and return the best common type. As an example, TypeScript compiles the following

var x = "" && 7;

with x's type as "number", even though x will be a string.

Oct 1, 2012 at 9:30 PM

The current typing rules are very much intentional because of common constructs like this:

var x = GetCustomer(...);
var name = x &&; // Note: no BCT exists!
if(name) { /* we want name to be of type 'string' here, not the BCT of (Customer, string) or 'any' */ }

In the case of ("" && 7), there's not really any common type other than 'any' we could assign anyway. Unfortunately, the way the && operator is actually used in idiomatic JS, there's no one rule that will accurately capture the semantics of real-world && usage (same for ||, but the rules are different because the common use cases are different).