Error when trying to use TypeScript instead of JavaScript: Expected 1-2 arguments but got 3.ts(2554)
P粉420958692
2023-09-03 19:03:22
<p>When we use JavaScript, we have a Vue.js 2.6 application, but some code is written in TypeScript. I don't know much about TypeScript and trying to rewrite the code using Axios. It looks like this: </p>
<p><strong>1) Caller: </strong></p>
<pre class="brush:php;toolbar:false;">try {
const params = {
id: 1,
inn: 2,
withReferences: true,
};
const result = await gpbApi.leadService.getPartnerReferences(params);
} catch (error) {
console.log('error = ', error);
}</pre>
<p><strong>2) Call: </strong></p>
<pre class="brush:php;toolbar:false;">async getPartnerReferences(params: any) {
if (!params) return;
const { data } = await axios.get(`${path}/GroupAccountService/PartnerReferences`, params, {
withCredentials: true
});
return data.data;
}</pre></p>
As Quentin pointed out in the comments, the axios documentation has one required parameter (url) and one optional parameter (config). Your code is passed three parameters, so the error is accurate, and the three-parameter
get
call is not doing what you'd expect in JS or TS.However, the
config
parameter accepts a key namedparams
, which is most likely the intended location of yourparams
. You can use the Javascript abbreviation just use the nameparams
instead ofparams: params
. This means your fix is simply to move theparams
to the inside (braces) of the object initializer.If this code worked before, the
params
may have been in the object initializer on the same line as the URL, but were mistakenly moved outside the URL.