将Axios与TypeScript一起使用的正确方法

将Axios与TypeScript一起使用的正确方法,typescript,axios,react-typescript,Typescript,Axios,React Typescript,我的应用程序中有一个我似乎无法解决的错误。 我将axios与TypeScript一起使用。 下面是我尝试执行的代码示例: export const fetchTransactions = (PageNum: number, PageSize: number, Context_id: number) => new Promise<Transaction[]> (async (resolve, reject) => { try { const respons

我的应用程序中有一个我似乎无法解决的错误。 我将
axios
TypeScript
一起使用。 下面是我尝试执行的代码示例:

export const fetchTransactions = (PageNum: number, PageSize: number, Context_id: number) => new Promise<Transaction[]> (async (resolve, reject) => {

  try
  {
    const response = await axios.post<AxiosResponse<Transaction[]>>(FETCH_TRANSACTIONS_URL, {PageNum, PageSize, Context_id})
    const {transactions} = response.data
    resolve(transactions)
  }
  catch (error) {
    reject(error.response);
  }
})
export const fetchTransactions=(PageNum:number,PageSize:number,Context\u id:number)=>新承诺(异步(解析,拒绝)=>{
尝试
{
const response=等待axios.post


如何删除此错误?正确的响应类型应该是什么?

通常,我会以这种方式使用axios和Typescript

const fetchTransactions = (PageNum: number, PageSize: number, Context_id: number): Promise<Transaction[]> =>
  axios
    .post<Transaction[]>(FETCH_TRANSACTIONS_URL, {PageNum, PageSize, Context_id})
    .then((response) => {
      if (response.status >= 200 && response.status < 300) {
        return response.data;
      }
      throw new Error(response.status.toString());
    })
    .catch(({ response }) => {
      throw new Error(response.status);
    });
const fetchTransactions=(PageNum:number,PageSize:number,Context\u id:number):Promise=>
axios
.post(FETCH_TRANSACTIONS_URL,{PageNum,PageSize,Context_id})
。然后((响应)=>{
如果(response.status>=200&&response.status<300){
返回响应数据;
}
抛出新错误(response.status.toString());
})
.catch({response})=>{
抛出新错误(response.status);
});

语法是对象解构语法。
{name}
是为了更容易地将属性从对象中获取到变量。
响应。
数据可能是一组
事务
的数组(因为这是您定义它的方式),它通常没有名为
transactions
的属性。我想您需要的是
const transactions=response.data;
,没有括号。@谢谢您的回复。
response.data
实际上是
{transactions:[]类型的对象
这就是我进行对象销毁的原因。然后响应的类型应该是
AxiosResponse
,这样
transactions
属性根据TypeScript存在。@HereticsMonkey你是说,
axios.post
还是
axios.post
?前者不起作用-我得到了相同的错误,后者起作用了。然后我是说后者;)。我不使用axios,所以我在猜测。您只需要在其中包含
事务
作为属性的东西。