Conditional expression (?) is the shorthand method of the if else statement in TypeScript. In the below program, I will create an enum object and then find out which enum value the user has entered into the function and display the message accordingly with conditional expression.
enum LightSwitches {
On = "On",
Off = "Off"
}
function lightOnOffed(state : LightSwitches) {
state == LightSwitches.On ? console.log(`Light's ${LightSwitches.On}!`) : console.log(`Light's ${LightSwitches.Off}!`)
}
lightOnOffed(LightSwitches.On) /* Light's On! */
lightOnOffed(LightSwitches.Off) /* Light's Off! */
The conditional expression statement above makes the program simpler than using the if-then-else expression.