A small snippet of code is breaking my head:
var label: number= 12354;
if(typeof(label) == 'number'){
label =label.toString();
}
But it’s giving an error in toString: The property ‘toString’ does not exist in type ‘never’. But it is a number! Why does inside if say it is of type never? If you change label to type any, the errors will be eliminated. I want to know why typescript ignored validation and stated something impossible in the presented context?
//Expectation
label = '12345';
//Reality:
Property 'toString' does not exist in type 'never'.ts(2339)
>Solution :
Try this way
var label: number | string = 12354;
if (typeof label === 'number') {
label = label.toString();