There's a function in my code that takes an argument with three possible values and returns a corresponding value based on this argument. Essentially, it can return one of three different values.
To achieve this, I've implemented a switch statement to determine the output value based on the input argument. This way, I have predefined safe values for each possible argument value.
However, when calling the function with an argument, TypeScript doesn't guarantee the exact returned value; it leaves room for the other two possibilities as well.
function get(value: "string" | "number" | "boolean") {
switch (value) {
case "string":
return "A";
case "number":
return 1;
case "boolean":
return true;
}
}
var test1 = get("number");
https://i.sstatic.net/y573v.png
How can I ensure safe values in this scenario? Should I consider using an enumeration? Your help would be greatly appreciated. Thank you!