I am unable to convert a boolean to a string value in TypeScript.
I have tried to use the toString() method but it does not seem to be implemented on bool.
Updated for comments!
You can now use toString() to get a string value from a boolean type.
var myBool: boolean = true;
var myString: string = myBool.toString();
console.log(myString);
This outputs:
"true"
And has no errors or warnings.
${myBool.toString().toUpperCase()}.For those looking for an alternative, another way to go about this is to use a template literal like the following:
const booleanVal = true;
const stringBoolean = `${booleanVal}`;
The real strength in this comes if you don't know for sure that you are getting a boolean value. Although in this question we know it is a boolean, thats not always the case, even in TypeScript(if not fully taken advantage of).
One approach is to use the Ternary operator:
myString = myBool? "true":"false";
"true" | "false"return Boolean(b) ? 'true':'false'
b is already a boolean, why use Boolean(b)?if you know your value is always true/false, you can use JSON.stringify(myBool) it will give you value like 'true' or 'false'
BooleansupportstoString.