Open In App

How to Use a Typescript Switch on Multiple Inputs ?

Last Updated : 05 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Using a TypeScript switch on multiple inputs involves combining input conditions into a single value or using nested switch statements. This approach streamlines logic for handling diverse input scenarios, ensuring efficient and structured code management based on input combinations.

Combining Inputs into a Single Value

Combining inputs into a single value simplifies handling multiple inputs by representing them as flags or bitwise combinations. This approach consolidates the inputs into one switch statement, enabling efficient and structured code management for diverse input scenarios.

Example: The below code will explain how you can combine multiple inputs and use them with switch statements.

JavaScript
enum Inputs {
    Input1 = 1,
    Input2 = 2,
    Input3 = 4,
}

function handleCombinedInput
    (combinedInput: Inputs[]): void {
    for (const input of combinedInput) {
        switch (input) {
            case Inputs.Input1:
                console.log("Handling Input1");
                break;
            case Inputs.Input2:
                console.log("Handling Input2");
                break;
            case Inputs.Input3:
                console.log("Handling Input3");
                break;
            default:
                console.log("Handling default case");
        }
    }
}

let combinedInput: Inputs[] =
    [Inputs.Input1, Inputs.Input2, Inputs.Input3];
handleCombinedInput(combinedInput);

Output:

Handling Input1
Handling Input2
Handling Input3

Using Nested Switch Statements

Nested switch statements handle multiple independent inputs by evaluating each input separately within its own switch block. This approach maintains clarity and separation of logic for each input, ensuring efficient and structured code management for diverse input scenarios.

Example: The below code uses nested switch statements to handle multiple inputs within them.

JavaScript
function handleInputCombination
    (input1: number, input2: number, input3: number):
    void {
    switch (input1) {
        case 1:
            switch (input2) {
                case 2:
                    switch (input3) {
                        case 3:
                            console.log
                                ("Handling input combination 1, 2, 3");
                            break;
                        default:
                            console.log
                                ("Handling default case for input3");
                            break;
                    }
                    break;
                default:
                    console.log
                        ("Handling default case for input2");
                    break;
            }
            break;
        default:
            console.log("Handling default case for input1");
            break;
    }
}

handleInputCombination(1, 2, 3);

Output:

Handling input combination 1, 2, 3

By combining inputs into a single value or using nested switch statements, you can efficiently handle multiple input scenarios in TypeScript. Each approach has its advantages, and choosing the right one depends on the specific requirements of your application.


Next Article

Similar Reads