It is generally considered bad form to use fall throughs in switch statements. We've even added a compiler option to prevent it completely: noFallthroughCasesInSwitch.
With this option enabled, we've declared that break is always and forever required in each and every case statement. Which makes the 'break' statement completely redundant.
Changing the semantics of the switch operator to automatically break would be shockingly counter to the principle of least surprise. Hence, I propose the addition of a new operator ('Decision-making construct'): the auto-breaking switch statement.
This behavior can be easily coded manually using either if/else blocks or even just living with the current behavior of case "x": doThing(); break;. This proposal is for convenience only.
1) Choose a name for the statement. switchb, bswitch, whatever, etc.
2) Because Typescript does not directly control the list of reserved words in ES, the feature would need to be enabled via a new compiler option, such as enableAutoBreakingSwitch
3) Perhaps propose that the name chosen be nominated for reserved-word status in ESNext+1. But that seems highly optimistic.
The auto-breaking switch will compile exactly as if the developer had added 'break;' as the last statement in each case block.
TypeScript design goals:
- Avoid adding expression-level syntax.
While this might be a handy feature, the right way to champion it in ECMAScript. Especially when it comes to things that don't have anything to do with types, the TypeScript team has been very reluctant to introduce new expression level syntax.
Most helpful comment
TypeScript design goals:
While this might be a handy feature, the right way to champion it in ECMAScript. Especially when it comes to things that don't have anything to do with types, the TypeScript team has been very reluctant to introduce new expression level syntax.