TypeScript: Given value of function is a number but returning value can be a string or boolean.
In this case the returning value type is Any which I do not want to use:
var getValue = (myVal: number) => {
if(myVal > 5){
return true;
}
return "200 OK";
}
In this case the returning value type is Boolean which cause error for the string:
var getValue = (myVal: number):boolean => {
if(myVal > 5){
return true;
}
return "200 OK";
}
In this case the returning value type is String which cause error for the boolean:
var getValue = (myVal: number):string => {
if(myVal > 5){
return true;
}
return "200 OK";
}
I have used the type Any in this scenario but as it is not a good practice so I just wanted to know which type can be used instead of Any .
You can use a union | like this...
var getValue = (myVal: number):boolean | string => {
if(myVal > 5){
return true;
}
return "200 OK";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.