134

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.

Ucodia
  • 7,410
  • 11
  • 47
  • 81
  • 2
    That's odd, the native JS `Boolean` supports [`toString`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Boolean/toString). – JohnnyHK Feb 08 '13 at 15:36
  • 1
    It seems that TypeScript definitely misses this basic implementation. – Ucodia Feb 08 '13 at 15:50

6 Answers6

183

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);
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 2
    This is an acknowledged bug in TypeScript and is apparently planned to be fixed in the next release (0.8.2) - http://typescript.codeplex.com/workitem/362 – Jon Apr 24 '13 at 14:06
  • 3
    toString() will definitely work fine as of 2016 (versions 1.6) – Steel Nation May 10 '16 at 14:34
  • 1
    flag: boolean = Boolean("true"); if you need to convert to boolean from string – sandeep talabathula Apr 21 '17 at 22:13
  • 4
    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. – KidKode Apr 19 '18 at 13:07
47

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

Jon
  • 2,456
  • 21
  • 28
23

One approach is to use the Ternary operator:

myString = myBool? "true":"false";
Tolga
  • 2,643
  • 1
  • 27
  • 18
  • One advantage of this approach is the converted string becomes literal types of `"true" | "false"` – catwith Apr 14 '23 at 08:29
3
return Boolean(b) ? 'true':'false'
Meredith
  • 3,928
  • 4
  • 33
  • 58
  • 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: https://stackoverflow.com/help/how-to-answer . Good luck – nima Oct 02 '21 at 09:03
  • If `b` is already a boolean, why use `Boolean(b)`? – General Grievance Feb 18 '22 at 05:26
1

This if you have to handle null values too:

stringVar = boolVar===null? "null" : (boolVar?"true":"false");
Luca C.
  • 11,714
  • 1
  • 86
  • 77
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'

Pargat
  • 1
  • 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](/help/how-to-answer). – Community Jul 12 '23 at 18:54