Alternative to Switch case in typescript


Hello Developer, how are you? I will present an alternative to using switch case.

Do you know what a switch case is? When a lot of else ifs appear, we soon think about using a switch case to solve our problem of many ifs.

The switch case approach may seem like the solution to many ifs, but is it really the solution?

The switch case has several problems, from its procedural control flow to its non-standardization in dealing with blocks. of code. Javascript uses keys, but switch does not. In your design we are forced to manually add the breaks via the word break.

Enum Color

Color

enum Color{
    RED = "RED",
    BLACK = "BLACK",
    GREEN = "GREEN"
}

With switch case

Method with switch

    getColorWithCode1(colors: Colors): string{
        let resultColor!: string;

        switch (colors.color){
            case Color.RED:
                resultColor = `Red: ${colors.code}`
                break;
            case Color.BLACK:
                resultColor = `Black: ${colors.code}`
                break;
            case Color.GREEN:
                resultColor = `Green: ${colors.code}`
                break;
        }

        return resultColor;
    }

Now with use type

add new Type ColorKeys

type ColorKeys = {
    [key in Color]: string;
}

Now use

    getColorWithCode2(colors: Colors): string{
        const helper: ColorKeys = {
            RED: `Red: ${colors.code}`,
            BLACK:`Black: ${colors.code}`,
            GREEN:`Green: ${colors.code}`
        }
        return helper[colors.color];
    }

Can you help me?

Download my games and rate them! it helps me a lot.

References