我正在使用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>'.
型
我不知道是什么问题,我已经尝试了不同的方法,但没有工作到目前为止。
这里是code
1条答案
按热度按时间yzuktlbb1#
由于axios的次要版本4(从
1.3.x
到1.4.x
),这个问题似乎正在发生。我自己也试着弄明白了,看到了axios的changelog,但这两个版本之间唯一的区别是后者导出了一个类型。更改为
1.3.x
版本应该可以成功编译库。