How to convert string to boolean in typescript Angular 4
Method 1 :
var stringValue = "true";
var boolValue = (/true/i).test(stringValue) //returns true
Method 2 :
var stringValue = "true";
var boolValue = (stringValue =="true"); //returns true
Method 3 :
var stringValue = "true";
var boolValue = JSON.parse(stringValue); //returns true
Method 4 :
var stringValue = "true";
var boolValue = stringValue.toLowerCase() == 'true'; //returns true
Method 5 :
var stringValue = "true";
var boolValue = getBoolean(stringValue); //returns true
function getBoolean(value){
switch(value){
case true:
case "true":
case 1:
case "1":
case "on":
case "yes":
return true;
default:
return false;
}
}
source: http://codippa.com/how-to-convert-string-to-boolean-javascript/
How can I convert a string to boolean in JavaScript?
Do:var isTrueSet = (myValue === 'true');
using the identity operator (===
), which doesn't make any implicit type conversions when the compared variables have different types.
This will set isTrueSet
to a boolean true
if the string is "true" and boolean false
if it is string "false" or not set at all.
Don't:
You should probably be cautious about using these two methods for your specific needs:
var myBool = Boolean("false"); // == true
var myBool = !!"false"; // == true
Any string which isn't the empty string will evaluate to true
by using them. Although they're the cleanest methods I can think of concerning to boolean conversion, I think they're not what you're looking for.
TypeScript: Convert a bool to string value
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);
Typescript conversion to boolean
You can use double exclamation sign trick which Typescript does allow and which works fine in JavaScript:
foo(!!xxx);
Alternatively, cast it to any
foo(<any>xxx);
Passing in boolean and returning a string in TypeScript
Is this what you want?
return bool ? 'YES' : 'NO';
Typescript generic function to return a string, number, or boolean
I think a crucial piece of information that you seem to be missing is that in typescript, all type information available at compile time is erased.
That means any kind of expression like T instanceof Number
can never work. T
is not a value, but a type, and after compiling, it just wouldn't be there any more, which is why the typescript compiler would refuse to compile it.
If you want the behavior of your function to change, depending on typing information, you have to pass that information in form of something that isn't just a type, but also a value.
Something already mentioned in the comments to your question is the concept of overloads. These are indeed a good way to solve the problem you're facing.
For instance, you could have a second argument that allows passing an optional type identifier.
This can then be used within the code of the function to change the output type.
By writing multiple type definitions for the function, typescript will be able to realize the output types depending on the input types you give to the function.
When using your get
function as an example, this is how it could look like:
function get(envVar: string): string;
function get(envVar: string, toType: "number"): number;
function get(envVar: string, toType: "boolean"): boolean;
function get(envVar: string, toType?: "number" | "boolean") {
const val = "Example value" as string;
if (toType === "number") {
return Number.parseInt(val);
}
else if (toType === "boolean") {
return Boolean(val); // return as boolean
}
else {
return val; // return as string
}
}
As you can see, this doesn't work via generics any more, but over multiple function signatures instead.
This also gives you the certainty that from within the function you actually return a suitable type, since typescript will otherwise complain that the "overload signature is not compatible with it's implementation"
How can i pass a boolean state in reactjs with typescript
Since the way you have defined types of props, you need to provide the props like below.
<Modal item={{item: dataDetail}} showModal={{showModal: false}} />
OR
Organize the interfaces like below.
- Remove
DetailProps
interface - Change the
ModalProps
to the following form
interface ModalProps {
item: ItemProps;
showModal: boolean;
}
- Define
ModalProps
as the prop type
export default function ModalComponent(props: ModalProps)
- In Order.tsx
<Modal item={dataDetail} showModal={false} />
Related Topics
How to Select Every 2Nd Element of an Array in a for Loop
Avoid a Link to Reload the Page in JavaScript
React Passing Import Name as Prop and Adding to Image Src
Updating Array in Object Properties Using Property'S Name
Angular 4 | Window.Scrollto(); Is Not Working Properly
Filter Input Text to Enter Only Number and Not Even Decimal
Remove Duplicate Objects from Json Array
Regex to Match 2 Different Words in a String
Different Color for Each Bar in a Bar Chart; Chartjs
Split String in JavaScript and Detect Line Break
How to Count Unique Value from Object of Array in JavaScript
Reprompt for Permissions With Getusermedia() After Initial Denial
How to Show a Confirm Message Before Delete
Get Request Works With Postman But Why Doesn't It Work With Reactjs Fetch
Howto Trigger Input Event Programmatically on Input Text Field
How to Close Bootstrap 3 Dropdown When We Click Outside on a Tablet
Ng2-Smart-Table Bind 'Add New' Button Event to an External Button
How to Match Only the Exact Word Using Indexof or Includes in JavaScript