Switch Case in TypeScript
-
Switch Case
Expression in TypeScript - Multiple Types in TypeScript
-
Use
Enums
WithSwitch Cases
in TypeScript -
Use
Switch Case
With Various Expressions in TypeScript
Switch case
is an important programming construct used for shifting between different parts of the code blocks based on different application logic. Different programming languages have their own implementation of the switch case
expressions.
TypeScript also has support for switch cases
with the expression having any type. In languages like C++, the switch
expression must necessarily be of type integer and match one of the cases.
Switch Case
Expression in TypeScript
Switch
cases are important for shifting between code blocks based on an expression. It can be used to avoid the complexities of multiple if-else
blocks.
The following shows the construct of a basic switch case
expression in TypeScript.
switch ( expression ){
case val1:
...
break;
case val2:
...
break;
case val3:
...
break;
default:
...
break;
}
Thus the expression is matched with one of the cases, and the following code block is executed. The code block corresponding to the default
block is executed if no match is found.
Unlike many programming languages, the expression in the switch
clause can have any type in TypeScript.
Multiple Types in TypeScript
In TypeScript, the switch
statement can have multiple types depending on the application. The following code blocks show how switch
statements can be executed in TypeScript.
function getStatus(status : string) {
switch ( status ) {
case "SUCCESS":
console.log("Operation successful");
break;
case "IN-PROGRESS":
console.log("Processing ...");
break;
case "FAILURE":
console.log("Operation failed");
break;
default:
console.log("Could not get status");
break;
}
}
getStatus("SUCCESS");
Output:
"Operation successful"
Thus the above code block uses a string to compare the varying cases in a switch
statement.
Use Enums
With Switch Cases
in TypeScript
Enums
are widely used while using switch cases
in TypeScript. The following shows how enums
can be used with switch-case
statements in TypeScript.
enum Status {
SUCCESS,
IN_PROGRESS,
FAILURE
}
function getStatus(status : Status) {
switch ( status ) {
case Status.SUCCESS:
console.log("Operation successful");
break;
case Status.IN_PROGRESS:
console.log("Processing ...");
break;
case Status.FAILURE:
console.log("Operation failed");
break;
default:
console.log("Could not get status");
break;
}
}
getStatus(Status.FAILURE);
Output:
"Operation failed"
Use Switch Case
With Various Expressions in TypeScript
Other than regular types, switch
statements can also be matched with two or more variables. A simple way is to do string concatenation among the two strings to match between two strings in the case
statements.
function getDirection(dir1 : string, dir2 : string) {
var direction : string = dir1 + ' ' + dir2;
switch ( direction ) {
case 'N W':
console.log('North-West');
break;
case 'N E':
console.log('North-East');
break;
case 'S W':
console.log('South-West');
break;
case 'S E':
console.log('South-East');
break;
default:
console.log("Default direction");
break;
}
}
getDirection('S', "E");
Output:
"South-East"