TypeScript: Convert a bool to string value

typescript convert string to boolean
convert string to boolean in node
boolean value of string js
convert boolean to string java
convert true false string to boolean js
typescript boolean
bool string to bool
javascript treat string as boolean

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 throught documentation and I could not find anything helpful and 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.

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

Boolean.prototype.toString(), For Boolean objects and values, the built-in toString method returns the string " true " or " false " depending on the value of the boolean object. The boolean is a primitive type in Typescript. It represents a simple true/false value. They are implemented as numerical values with a single binary digit (i.e., 0 & 1). The Boolean is an object wrapper for a boolean value. Also, let us find the difference between Boolean vs boolean. We also learn how to convert boolean to string and string to

One approach is to use the Ternary operator:

myString = myBool? "true":"false";

JavaScript Boolean toString() Method, Example. Convert a Boolean value to a string: var bool = true; var x = bool.​toString(); The toString() method returns a boolean value as a string. Note: This​  This is one more way of converting boolean string values to string format. parse method of JSON object which accepts string returns true if the value is ‘true’ returns false if the value is ‘false’ throws an exception if the value is string other than boolean values We have written function to return undefined for normal strings.

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

How to Convert String to boolean or boolean to string in typescript , This is one more way of converting boolean string values to string format. parse method of JSON object which accepts string returns true if the value is 'true' returns  Boolean(input) will always return true as long as the string is not empty. So you are basically just doing let boolVar = (input === 'true'), which is the question in the first place. And even if it would work the way you expect, then still it doesn't make a difference between false and a non-boolean string. – Ivar May 17 '17 at 13:08

This if you have to handle null values too:

stringVar = boolVar===null? "null" : (boolVar?"true":"false");

Converting a value to string in JavaScript, Cover of book “Tackling TypeScript” As a way for converting a value to string, I find it less descriptive of one's intentions. If, however, an object is an instance of Boolean, Number or String, then valueOf() will return a  When you create a new Boolean object from a string, it doesn’t try to check whether the string equals “true” or “false”. Instead, rather misleadingly, it checks whether the variable is a non-falsy value (e.g. a value that evalutes to false–0, undefined, an empty string, null, etc).

Typescript Boolean, The Boolean is an object wrapper for a boolean value. Also, let We also learn how to convert boolean to string and string to boolean. Table of  The toString () method returns a boolean value as a string. Note: This method is called by JavaScript automatically whenever a boolean is used in a string operation. Browser Support. boolean .toString () Technical Details. A String, either "true" or "false" JavaScript Version: JavaScript Tutorial: JavaScript Booleans.

JavaScript - Boolean toString() Method, JavaScript - Boolean toString() Method - This method returns a string of either true or false depending upon the value of the object. The only way I could find to determine the field's boolean value, once it was converted into a string, was to depend upon the literal value of its string representation. var myValue = document.myForm.IS_TRUE.value; var isTrueSet = myValue == 'true'; Is there a better way to accomplish this? improve this question. asked Nov 5 '08 at 0:13.

JavaScript, Sometimes it is needed to convert a string representing boolean value “true”, “​false” into intrinsic type of JavaScript. Given a string and the task is to convert  Angular: Convert input values to the correct type with a TypeScript property decorator Published 2016-02-24 I recently played with Angular and found an uncomfortable problem: Declaring an input property with a TypeScript type does not guarantee that the input value will always be of this type.

Comments
  • That's odd, the native JS Boolean supports toString.
  • It seems that TypeScript definitely misses this basic implementation.
  • 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/362
  • toString() will definitely work fine as of 2016 (versions 1.6)
  • flag: boolean = Boolean("true"); if you need to convert to boolean from string
  • 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.