TypeScript: Convert a bool to string value
Asked Answered
C

6

137

I am unable to convert a boolean to a string value in TypeScript.

I have been roaming through documentation and I could not find anything helpful. I have tried to use the toString() method but it does not seem to be implemented on bool.


Edit: I have almost no JavaScript knowledge and came to TypeScript with a C#/Java background.

Carine answered 8/2, 2013 at 14:37 Comment(2)
That's odd, the native JS Boolean supports toString.Bronk
It seems that TypeScript definitely misses this basic implementation.Carine
S
190

This is either a bug in TypeScript or a concious design decision, but you can work around it using:

var myBool: bool = true;
var myString: string = String(myBool);
alert(myString);

In JavaScript booleans override the toString method, which is available on any Object (pretty much everything in JavaScript inherits from Object), so...

var myString: string = myBool.toString();

... should probably be valid.

There is also another work around for this, but I personally find it a bit nasty:

var myBool: bool = true;
var myString: string = <string><any> myBool;
alert(myString);
Spandau answered 8/2, 2013 at 16:34 Comment(4)
This is an acknowledged bug in TypeScript and is apparently planned to be fixed in the next release (0.8.2) - typescript.codeplex.com/workitem/362Serf
toString() will definitely work fine as of 2016 (versions 1.6)Heartburning
flag: boolean = Boolean("true"); if you need to convert to boolean from stringDam
Apparently even for TS Version 2.9.0-dev.20180327 toString() will definitely NOT work! Had to use @Fenton's example here for it to work. WEIRD but fact.Philippeville
S
49

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).

Serf answered 9/8, 2018 at 22:58 Comment(0)
W
23

One approach is to use the Ternary operator:

myString = myBool? "true":"false";
Warr answered 30/12, 2017 at 2:5 Comment(1)
One advantage of this approach is the converted string becomes literal types of "true" | "false"Piapiacenza
I
3
return Boolean(b) ? 'true':'false'
Invade answered 1/10, 2021 at 13:46 Comment(2)
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: stackoverflow.com/help/how-to-answer . Good luck 🙂Keefer
If b is already a boolean, why use Boolean(b)?Earflap
C
1

This if you have to handle null values too:

stringVar = boolVar===null? "null" : (boolVar?"true":"false");
Cherie answered 20/7, 2018 at 10:28 Comment(0)
B
0

if you know your value is always true/false, you can use JSON.stringify(myBool) it will give you value like 'true' or 'false'

Bionics answered 10/7, 2023 at 18:50 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Sondrasone

© 2022 - 2024 — McMap. All rights reserved.