Something unusual happened with Typescript - I assigned a string value to a boolean variable, but no error was generated.
I purposely triggered an error in order to observe how Typescript would react, only to find that it did not produce the expected error message.
To investigate further, I created a class with a boolean attribute, then defined an interface and a constructor method to initialize the variable. However, when I received a JSON object with the variable as type string, the boolean variable accepted the string value without any errors being raised. This goes against the intended data type validation in my code.
export class Product{
public status:boolean;
constructor(obj?: IProduct) {
if(obj)
{
this.status = obj.status;
}
}
}
interface IProduct {
status:boolean;
}
In the JSON response from the server:
{status: "test"}
The 'test' string is incorrectly being allocated to the boolean 'status' variable.
Is there a way to enforce strict type checking for this variable to ensure it only accepts boolean values? Any insights or suggestions are greatly appreciated.