Home > Net >  When try to use TypeScript instead of JavaScript get an error: Expected 1-2 arguments, but got 3.ts(
When try to use TypeScript instead of JavaScript get an error: Expected 1-2 arguments, but got 3.ts(

Time:02-02

We have Vue.js 2.6 app when we use JavaScript, but there's some code written in TypeScript. I'm not much in TypeScript and try to rewrite the code that uses Axios. It looks as following:

1) Caller:

try {
  const params = {
    id: 1,
    inn: 2,
    withReferences: true,
  };
  const result = await gpbApi.leadService.getPartnerReferences(params);
} catch (error) {
  console.log('error = ', error);
}

2) Call:

async getPartnerReferences(params: any) {
  if (!params) return;
  const { data } = await axios.get(`${path}/GroupAccountService/PartnerReferences`, params, { 
    withCredentials: true
  });
  return data.data;
}

CodePudding user response:

As Quentin points out in the comments, the axios documentation has one required parameter (url) and one optional parameter (config). Your code passes three parameters, so the error is accurate, and the three-parameter get call wasn't doing what you were expecting in either JS or TS.

However, the config parameter accepts a key called params, which is likely where your params were intended to go. You can use Javascript shorthand to just use the name params rather than params: params. This means your fix is just to move the params to the inside of the object initializer (curly braces).

If this code worked before, it's possible that params was once in the object initializer on the same line as the URL but was mistakenly moved outside of it.

async getPartnerReferences(params: any) {
  if (!params) return;
  const { data } = await axios.get(`your.url`, {
    params, // this is now a part of the config object
    withCredentials: true
  });
  return data.data;
}
  • Related