Typescript does not generate an error when I assign a string value to a boolean variable.
I forced an error to see how the typescript behaved and it did not generate the error that I expected.
I created a class with an attribute of type boolean. Then I created an interface. And I created the constructor method to initialize the variable.So I get a json where the variable is of type string. To my surprise the boolean variable receives the string. I wish it would generate an error. My code does not respect the type of the variable.
export class Product{
public status:boolean;
constructor(obj?: IProduct) {
if(obj)
{
this.status = obj.status;
}
}
}
interface IProduct {
status:boolean;
}
my json of server
{status: "test"}
The test value is assigned to the status variable. And this should not be allowed.
Does anyone know how I can ensure that the variable only receives boolean value?