Typescript switch case - top examples


In this blog post, I will take you through the various examples of typescript switch case.

Typescript switch case examples

Typescript is object oriented language,super power of a javascript which includes many language features. Switch statement is one the basic block of programming langauge. which is used for conditional expression to avoid nested if else statements.

Here is a basic syntax

switch(expression){
    case case1: statements
        break;
    case case2: statements
        break;
    default:
        default statements;
}

You can check my previous post about typescript switch case complete tutorial

Let us see various examples in switch case of typescript

string variable in switch and case example

String and case statements accepts of variable and data of string type. The type of the value should be primitive string, not global object String type

Important points In the below example

  • Switch expression accepts variable of string data type
  • In each case the value of type is string
  • each case returns value
  • each case has break statement to exit from the loop
class SwitchExample {

	public isWorkingDay(weekName: string): boolean {
		switch (weekName) {
			case 'saturday':
				console.log('saturday')
				return false;
				break;
			case 'sunday':
				console.log('sunday')
				return false;


				break;

			default:
				console.log('working day')
				return true;


				break;
		}
	}
}

var example = new SwitchExample();
console.log(example.isWorkingDay('sunday'))
console.log(example.isWorkingDay('monday'))

output

sunday
false
working day
true

primitive data type values in switch case

Switch cases accepts primitive types numbers as well as expressions

The below is an example to check given number is positive or negative using typescript switch case

expression accepts contional express which returns true or false, Each case block must be provided with boolean value

class SwitchNumberExample {

	public isPositive(value: number): boolean {
		switch (value < 0) {
			case false:
				console.log('negative number')
				return false;
				break;
			default:
				console.log('positive number')
				return true;


				break;
		}
	}
}

var example = new SwitchNumberExample();
console.log(example.isPositive(1))
console.log(example.isPositive(-1))

Here is an output

negative number
false
positive number
true

Switch case union type with Object and interface

The below example uses below things

  • Created marker interfaces which has no methods
  • provided class for Animla and Lion by implementing Animal
  • Each class has descriminator type
  • Switch case accepts union type of Dog and Lion
  • Union type expression is evaluated against stiring type case value
interface Animal {}

class Dog implements Animal {
  type: string="dog";
}

class Lion implements Animal {
   type: string= "lion";
}
class SwitchClassExample {

	public getAnimal(animal: Dog | Lion) {
		switch (animal.type) {
			case 'dog':
				console.log('Animal is Dog ');
				break;
			case 'lion':
				console.log('Animal is Lion ');
				break;

			default:
				console.log('Other Animal types');
				break;
		}
	}
}

var example = new SwitchClassExample();
let animal=new Dog();
example.getAnimal(animal);
example.getAnimal(new Lion());

and given output is

Animal is Dog
Animal is Lion
Similar Posts