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

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

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:

  1. // 在使用Node和TypeScript创建库。我在我的`api.ts`文件中有这段代码
  2. import axios, { AxiosInstance } from 'axios';
  3. import {
  4. IAxiosStruct,
  5. BaseError,
  6. BASE_URL,
  7. excludeFields,
  8. handleAxiosError,
  9. } from './utils';
  10. export class TermiiCore {
  11. public request: AxiosInstance;
  12. constructor(public apiKey: string) {
  13. this.apiKey = apiKey;
  14. this.request = axios.create({
  15. baseURL: BASE_URL,
  16. headers: {
  17. Accept: 'application/json',
  18. 'Content-Type': 'application/json',
  19. },
  20. });
  21. }
  22. public async useRequest(req: IAxiosStruct) {
  23. try {
  24. const customHeaders = excludeFields(
  25. ['common', 'delete', 'get', 'head', 'put', 'patch', 'post'],
  26. this.request.defaults.headers
  27. );
  28. const getUrl = this.request.defaults.baseURL;
  29. const requestInstance = await axios.request({
  30. url: `${getUrl}${req.url}`,
  31. method: req.method,
  32. headers: customHeaders,
  33. data: req.data,
  34. });
  35. return requestInstance;
  36. } catch (error) {
  37. throw new BaseError({ message: handleAxiosError(error) });
  38. }
  39. }
  40. }
  1. // 并在`message.ts`中使用它
  2. import { TermiiCore } from '../../api';
  3. import { BaseError, handleAxiosError } from '../../utils';
  4. import { SendMessageDto } from './message.dto';
  5. export class Message extends TermiiCore {
  6. constructor(apiKey: string) {
  7. super(apiKey);
  8. }
  9. public async sendSMS(data: SendMessageDto) {
  10. try {
  11. const requestObj = {
  12. method: 'POST',
  13. url: `/sms/send`,
  14. data,
  15. };
  16. const response = await this.useRequest(requestObj);
  17. return response;
  18. } catch (error) {
  19. throw new BaseError({ message: handleAxiosError(error) });
  20. }
  21. }
  22. }

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

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

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

这是您提供的代码链接

  1. 请注意,这是您提供的原始代码的中文翻译。如果您需要进一步的帮助来解决错误,请提供更多上下文或详细信息。
  2. <details>
  3. <summary>英文:</summary>
  4. 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
)

  1. const getUrl = this.request.defaults.baseURL
  2. const requestInstance = await axios.request({
  3. url: `${getUrl}${req.url}`,
  4. method: req.method,
  5. headers: customHeaders,
  6. data: req.data,
  7. })
  8. return requestInstance
  9. } catch (error) {
  10. throw new BaseError({ message: handleAxiosError(error) })
  11. }

}
}

  1. 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,
}

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

}
}

  1. 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>'.

  1. I&#39;m not sure what the issue is, I have tried different approaches but nothing works so far.
  2. Here is the [code](https://github.com/peoray/termii-node-sdk/tree/switch-messaging)
  3. </details>
  4. # 答案1
  5. **得分**: 1
  6. axios 的次要版本从 `1.3.x` `1.4.x`(从`1.3.x``1.4.x`)以来,似乎发生了这个问题。我尝试自行解决,并查看了 axios 的[更新日志](https://github.com/axios/axios/blob/v1.x/CHANGELOG.md),但版本之间唯一的区别是后者导出了一个类型。
  7. 将版本更改为 `1.3.x` 应该可以使您的库成功编译。
  8. <details>
  9. <summary>英文:</summary>
  10. 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.
  11. Changing to version `1.3.x` should make your library compile successfully.
  12. </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:

确定