Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument type Number is not assignable to parameter type String|Function

var str = name.toUpperCase();
var ch = new Array();
ch = str.split('');

for(var i=0;i<7;i++)
{
  if(ch = null) {
    result_code.replace(
      pos.toString()+pos.toString()+pos.toString()+pos.toString(),
      "FFFF");
  } else {
    var temp = parseInt(ch[i]);
    var temp_integer = 64;
    if(temp<=122 & temp>=97) {
      var pos = i+1;
      result_code.replace(
        pos.toString()+pos.toString()+pos.toString()+pos.toString(),
        (temp - temp_integer)+40);
    }
  }
}

This code is creating the error at this line result_code.replace(pos.toString()+pos.toString()+pos.toString()+pos.toString(), (temp - temp_integer)+40);.

The underlined information is this section (temp - temp_integer)+40.

The error shown is Argument type Number is not assignable to parameter type String|Function.

What is wrong with this code? I am using WebStorm. I am likely just making a dumb mistake. Thanks in advance!

like image 239
Chris Avatar asked Jun 28 '13 01:06

Chris


People also ask

How do I fix the Argument of type string is not assignable to parameter of type never?

The error "Argument of type is not assignable to parameter of type 'never'" occurs when we declare an empty array without explicitly typing it and attempt to add elements to it. To solve the error, explicitly type the empty array, e.g. const arr: string[] = []; .

Is not assignable to parameter of type undefined?

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

Is not assignable to type null?

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

Is not assignable to parameter of type Setstate action never?

The error "Type is not assignable to type 'never'" occurs when we declare an empty state array with the useState hook but don't type the array. To solve the error, use a generic to type the state array, e.g. const [arr, setArr] = useState<string[]>([]) .


2 Answers

The replace method accepts a string or a function as second parameter. Turn your value into a string: ((temp - temp_integer)+40).toString().

like image 65
Guffa Avatar answered Oct 04 '22 15:10

Guffa


(temp - temp_integer)+40

is a numeric value and replace wants a string. Just use:

(temp - temp_integer)+40+""

assuming that you want the string representation of the number (eg, 65 becomes "65"). If you want the character at that code point (65 becomes "A"), you should look into using String.fromCharCode().

like image 22
paxdiablo Avatar answered Oct 04 '22 13:10

paxdiablo