TypeScript: Convert a bool to string value

Ucodia picture Ucodia · Feb 8, 2013 · Viewed 89.4k times · Source

I have a really simple issue, I can't get to convert a simple boolean to a string value in TypeScript.

I have been roaming through documentation and I could not find anything helpful. Of course I 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.

Answer

Fenton picture Fenton · Feb 8, 2013

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