Typescript Type 'number' is not assignable │ to type 'string' Typescript Type 'number' is not assignable │ to type 'string' angularjs angularjs

Typescript Type 'number' is not assignable │ to type 'string'


The two lines:

let result = `${integer}${fraction}`;result = parseFloat(result);

are the problem. Typescript is pretty good about infering the type of a variable when it's not explicitly declared. In this case, because you assign a string to the result, typescript infers it's type as a string. To fix this, you have two options. First, explicitly declare the type of that variable so that it allows both strings and numbers:

let result: string|number = `${integer}${fraction}`;result = parseFloat(result); // now should be ok.

Or you can assign the parsed number to a new variable, instead of reusing the result variable:

let result = `${integer}${fraction}`;let numberResult = parseFloat(result); // now should be ok.


You can't assign different types to a variable in Typescript. If you initialized the variable with a string it must remain a string.

    let result = `${integer}${fraction}`;    let resultAsNumber = parseFloat(result);     return resultAsNumber

This is a common cause of errors and the type system tries to prevent you from doing this.


There is an obvious error, you are trying to assign a number to a string variable, no need to parse to Float again, just return the number by parsing it

let result = `${integer}${fraction}`;  // at this point result = "100.55";return parseFloat(result);