Request: add strongly typed logical operators

Topics: General, Language Specification
Apr 11, 2013 at 11:55 PM
Problem: JavaScript's dreadful notion of "truthiness" means all logical operators return type any rather than type bool. This causes the type checker to miss a very large class of errors. Moreover, TypeScript's interpretation of any as a "get out of gaol free" card means these errors silently propagate into the rest of one's code.

Suggestion: add new infix logical operators (e.g., and, or, not) to TypeScript which both expect bool arguments and return bool results.

Yes, I could add utility functions to my programs with these signatures, but writing logic entirely in standard functional notation (f(...)) quickly becomes unreadable.
Apr 12, 2013 at 2:11 PM
One thing we're strongly considered as we go forward is a supported mode in the compiler that will warn/error on any inferred 'any' type. Some examples:

function f(x) { return x; } // warning: parameter x is inferred to 'any'
var y; //warning variable y is inferred to 'any'

In your example, if you use two typed arguments to '||', if the result type went to 'any', it could give a warning. Some operations wouldn't be caught, like 'not', but this would at least give you a better chance of catching more bugs early.
Apr 12, 2013 at 10:38 PM
Now that would be very welcome indeed. Lack of it substantially reduces the protection afforded by static type checking. On our project, it's even become the norm to use these little JavaScript "cheats" and it's already costing us.