Argument of type is not assignable to parameter of type ‘CreateAxiosDefaults.

huangapple go评论131阅读模式
英文:

Argument of type is not assignable to parameter of type 'CreateAxiosDefaults

问题

I apologize for the confusion. Here's the translation of the code you provided:

// 在使用Node和TypeScript创建库。我在我的`api.ts`文件中有这段代码

import axios, { AxiosInstance } from 'axios';
import {
  IAxiosStruct,
  BaseError,
  BASE_URL,
  excludeFields,
  handleAxiosError,
} from './utils';

export class TermiiCore {
  public request: AxiosInstance;

  constructor(public apiKey: string) {
    this.apiKey = apiKey;
    this.request = axios.create({
      baseURL: BASE_URL,
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
    });
  }

  public async useRequest(req: IAxiosStruct) {
    try {
      const customHeaders = excludeFields(
        ['common', 'delete', 'get', 'head', 'put', 'patch', 'post'],
        this.request.defaults.headers
      );

      const getUrl = this.request.defaults.baseURL;
      const requestInstance = await axios.request({
        url: `${getUrl}${req.url}`,
        method: req.method,
        headers: customHeaders,
        data: req.data,
      });
      return requestInstance;
    } catch (error) {
      throw new BaseError({ message: handleAxiosError(error) });
    }
  }
}
// 并在`message.ts`中使用它

import { TermiiCore } from '../../api';
import { BaseError, handleAxiosError } from '../../utils';
import { SendMessageDto } from './message.dto';

export class Message extends TermiiCore {
  constructor(apiKey: string) {
    super(apiKey);
  }
  public async sendSMS(data: SendMessageDto) {
    try {
      const requestObj = {
        method: 'POST',
        url: `/sms/send`,
        data,
      };

      const response = await this.useRequest(requestObj);
      return response;
    } catch (error) {
      throw new BaseError({ message: handleAxiosError(error) });
    }
  }
}

然而,当我运行pnpm run build时,我收到以下错误信息

Argument of type '{ baseURL: string; headers: { Accept: string; 'Content-Type': string; }; }' is not assignable to parameter of type 'CreateAxiosDefaults<any>'.
  Property 'string' is missing in type '{ baseURL: string; headers: { Accept: string; 'Content-Type': string; }; }' but required in type 'CreateAxiosDefaults<any>'.

我不确定问题出在哪里,我尝试了不同的方法,但目前没有任何有效的解决方案。

这是您提供的代码链接


请注意,这是您提供的原始代码的中文翻译。如果您需要进一步的帮助来解决错误,请提供更多上下文或详细信息。

<details>
<summary>英文:</summary>

I&#39;m creating a lib using Node and Typescript. I have this code in my `api.ts` file

import axios, { AxiosInstance } from 'axios'
import {
IAxiosStruct,
BaseError,
BASE_URL,
excludeFields,
handleAxiosError,
} from './utils'

export class TermiiCore {
public request: AxiosInstance

constructor(public apiKey: string) {
this.apiKey = apiKey
this.request = axios.create({
baseURL: BASE_URL,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
}

public async useRequest(req: IAxiosStruct) {
try {
const customHeaders = excludeFields(
['common', 'delete', 'get', 'head', 'put', 'patch', 'post'],
this.request.defaults.headers
)

  const getUrl = this.request.defaults.baseURL
  const requestInstance = await axios.request({
    url: `${getUrl}${req.url}`,
    method: req.method,
    headers: customHeaders,
    data: req.data,
  })
  return requestInstance
} catch (error) {
  throw new BaseError({ message: handleAxiosError(error) })
}

}
}


and using it in `message.ts`

import { TermiiCore } from '../../api'
import { BaseError, handleAxiosError } from '../../utils'
import { SendMessageDto } from './message.dto'

export class Message extends TermiiCore {
constructor(apiKey: string) {
super(apiKey)
}
public async sendSMS(data: SendMessageDto) {
try {
const requestObj = {
method: 'POST',
url: /sms/send,
data,
}

  const response = await this.useRequest(requestObj)
  return response
} catch (error) {
  throw new BaseError({ message: handleAxiosError(error) })
}

}
}


However, when I run `pnpm run build`, I get

Argument of type '{ baseURL: string; headers: { Accept: string; 'Content-Type': string; }; }' is not assignable to parameter of type 'CreateAxiosDefaults<any>'.
Property 'string' is missing in type '{ baseURL: string; headers: { Accept: string; 'Content-Type': string; }; }' but required in type 'CreateAxiosDefaults<any>'.


I&#39;m not sure what the issue is, I have tried different approaches but nothing works so far.

Here is the [code](https://github.com/peoray/termii-node-sdk/tree/switch-messaging)

</details>


# 答案1
**得分**: 1

自 axios 的次要版本从 `1.3.x` 到 `1.4.x`(从`1.3.x`到`1.4.x`)以来,似乎发生了这个问题。我尝试自行解决,并查看了 axios 的[更新日志](https://github.com/axios/axios/blob/v1.x/CHANGELOG.md),但版本之间唯一的区别是后者导出了一个类型。

将版本更改为 `1.3.x` 应该可以使您的库成功编译。

<details>
<summary>英文:</summary>

Since the minor version 4 for axios (from `1.3.x` to `1.4.x`) this issue seems to be happening. I&#39;ve tried to figure it out myself and saw the [changelog](https://github.com/axios/axios/blob/v1.x/CHANGELOG.md) for axios, but the only difference between the versions is that the latter one has exported a type.

Changing to version `1.3.x` should make your library compile successfully.

</details>



huangapple
  • 本文由 发表于 2023年8月5日 12:05:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定