How can I convert a string to a boolean in TypeScript for an Angular 4 application?
I am working with local storage to store and retrieve boolean values as strings. Here’s how I’m doing it:
In app.component.ts:
localStorage.setItem('CheckOutPageReload', this.btnLoginNumOne + ',' + this.btnLoginEdit);
Here, this.btnLoginNumOne and this.btnLoginEdit are string values, either “true” or “false”.
In mirror.component.ts, I retrieve and process these values:
if (localStorage.getItem('CheckOutPageReload')) {
let stringToSplit = localStorage.getItem('CheckOutPageReload');
this.pageLoadParams = stringToSplit.split(',');
this.btnLoginNumOne = this.pageLoadParams[0]; // Error: boolean value is not assignable to string
this.btnLoginEdit = this.pageLoadParams[1]; // Error: boolean value is not assignable to string
}
In this component, this.btnLoginNumOne and this.btnLoginEdit are expected to be boolean values.
I have tried various solutions, but none have worked. How can I correctly convert the string values from local storage to boolean values in TypeScript? Please provide guidance on how to handle this conversion using the keyword typescript string to boolean.