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:
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:
- Error TS2307 Cannot find module @pnp/sp/presets/all in SPFx
- SPFx – Bind dropdown list from SharePoint list using PnP
- SharePoint online spfx script editor web part
- How to Send Email with Attachments using Graph API in SPFx (React JS)
- Customize SharePoint Online site header and footer using SPFx Application Customizer
- How to use bootstrap in SPFx webpart
- SharePoint framework Local Workbench is showing empty
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’.
After working for more than 15 years in Microsoft technologies like SharePoint, Office 365, and Power Platform (Power Apps, Power Automate, and Power BI), I thought will share my SharePoint expertise knowledge with the world. Our audiences are from the United States, Canada, the United Kingdom, Australia, New Zealand, etc. For my expertise knowledge and SharePoint tutorials, Microsoft has been awarded a Microsoft SharePoint MVP (9 times). I have also worked in companies like HP, TCS, KPIT, etc.