type ‘string’ is not assignable to type ‘number’.ts

In this tutorial, we will see how to fix error type ‘string’ is not assignable to type ‘number’.ts and also how to fix error, error ts2345 argument of type ‘string’ is not assignable to parameter of type ‘number’.

We will also see how to convert string to number in typescript?

Also it fix error:

  • error ts2345 argument of type ‘string number’ is not assignable to parameter of type ‘number’
  • error ts2345 argument of type ‘string number’ is not assignable to parameter of type ‘string’
  • argument of type ‘string number’ is not assignable
  • error ts2345 argument of type ‘string’ is not assignable to parameter of type ‘never’
  • error ts2345 argument of type ‘string’ is not assignable to parameter of type
  • Argument of type ‘string | number | string[]’ is not assignable to parameter of type ‘number’

error ts2345 argument of type ‘string’ is not assignable to parameter of type ‘number’

While working with a SPFx CRUD operations example, I got the below error as

Argument of type ‘string | number | string[]’ is not assignable to parameter of type ‘number’.

type ‘string’ is not assignable to type ‘number’

The code I wrote as the below:

web.lists.getByTitle("ProjectDetails").items.getById($('input[name="itemID"]:checked').val()).delete()

The error was coming exactly on this $(‘input[name=”itemID”]:checked’).val().

Here the getById method expects an integer value. But $(‘input[name=”itemID”]:checked’).val() was returning a string value.

You can see it looks like below:

type 'string' is not assignable to type 'number'.ts
type ‘string’ is not assignable to type ‘number’.ts

To fix the issue I modified the code like below:

let id:number=Number($('input[name="itemID"]:checked').val());

await web.lists.getByTitle("ProjectDetails").items.getById(id).delete()

We can use the Number to convert string to number in typescript like:

Number('10')

This will return 10.

And if you will pass a string as a parameter like below, it will return NaN.

Number('Bijay')

This will return NaN.

You may like the following tutorials:

In this tutorial, we learned how to fix error, type ‘string’ is not assignable to type ‘number’.ts or error ts2345 argument of type ‘string’ is not assignable to parameter of type ‘number’ and Argument of type ‘string | number | string[]’ is not assignable to parameter of type ‘number’.

>