Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am currently knee deep in making a currency formatter directive for an Angular 4 app. on the parse strip out everything other than the numbers and the decimal and end up with a stringified float, but I need it to return as a float so I can do math with it.

parse(value: string, fractionSize: number = 2): number {
  let val = value.replace(/([^0-9.])+/ig, '');
  let [ integer, fraction = "" ] = (val || "").split(this.DECIMAL_SEPARATOR);
  integer = integer.replace(new RegExp(this.THOUSANDS_SEPARATOR, "g"), "");
  fraction = parseInt(fraction, 10) > 0 && fractionSize > 0
    ? this.DECIMAL_SEPARATOR + (fraction + PADDING).substring(0, fractionSize)
    : "";
  let result = `${integer}${fraction}`;
  // at this point result = "100.55";
  result = parseFloat(result); // this refuses to compile saying "Type 'number' is not assignable │ to type 'string'"
  return result;
}
like image 815
Bird Dad Avatar asked Feb 07 '18 17:02

Bird Dad


People also ask

Is not assignable to parameter of type string?

The error "Argument of type string | undefined is not assignable to parameter of type string" occurs when a possibly undefined value is passed to a function that expects a string . To solve the error, use a type guard to verify the value is a string before passing it to the function.

How do I convert a number to a string in TypeScript?

var num = new Number(10); console. log(num. toString()); console.

Is not assignable to type string Ts?

The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type string to something that expects a different type, e.g. a more specific string literal type or an enum. To solve the error use a const or a type assertion.

How do I fix type string undefined is not assignable to type string?

The "Type 'string | undefined' is not assignable to type string" error occurs when a possibly undefined value is assigned to something that expects a string . To solve the error, use the non-null assertion operator or a type guard to verify the value is a string before the assignment.


2 Answers

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.
like image 69
CRice Avatar answered Sep 22 '22 11:09

CRice


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.

like image 22
Titian Cernicova-Dragomir Avatar answered Sep 22 '22 11:09

Titian Cernicova-Dragomir