mardi 26 mai 2020

Why TypeScript disallows "thenless" branching?

It is known that TypeScript does not support the if statement without the body:

if (true);          // error
if (false); else {} // also error

It might be very annoying in some specific cases. For instance, in JavaScript this might help eliminate a pair of brackets while negating the condition:

if (x in y); else {
}
if (x instanceof X); else {
}
// vs
if (!(x in y)) {
}
if (!(x instanceof X)) {
}

Yes, it is still possible to declare an empty body like this:

if (x in y) {} else {
}

But I am wondering: is there any good reasons for such behavior?

I am not asking how to overcome this nicely, but just interesting about the motivation behind this "feature". Of course, except for a fear for missing the body by mistake.

Thanks.

Aucun commentaire:

Enregistrer un commentaire