【问题标题】:Type 'AxiosResponse<any>' is missing the following properties from type 'countries[]': length, pop, push, concat类型“AxiosResponse<any>”缺少类型“国家 []”中的以下属性:长度、弹出、推送、连接
【发布时间】:2021-10-29 17:17:14
【问题描述】:

在 setState 我收到这个 TS 错误:

“类型 'AxiosResponse' 缺少来自类型 'countries[]' 的以下属性:length、pop、push、concat 和 26 more.ts(2740)”

谁能建议哪里出了问题/在哪里使用 :countries[] ?

export interface IProfileEditorState {
  countries: countries[];
}

interface countries {
  countryID: string;
  countryName: string;
  label: astring;
  value: string;
  isDirector: number;
}

// Temp
public getCountrys() {
axios
.get('https://func-myportal-dev.azurewebsites.net/api/GetCountries?code=j0xGK8L6iRXpZqR3DgaDRoI/H/qjGZSGdBPIY9rZU84uH4SUa80noQ==')
.then(res => {
 this.setState({countries: res});
 })
 // Error catching
 .catch(error => this.setState({ error, isLoading: false }));
}

【问题讨论】:

    标签: typescript axios


    【解决方案1】:

    每个Axios method 都使用generic types,因此您可以执行以下操作:

    axios
      .get<countries>('https://func-myportal-dev.azurewebsites.net/api/GetCountries?code=j0xGK8L6iRXpZqR3DgaDRoI/H/qjGZSGdBPIY9rZU84uH4SUa80noQ==')
      .then(res => { /* res.data here is countries type */ })
    

    【讨论】:

      猜你喜欢
      • 2022-11-28
      • 2020-07-20
      • 2021-02-15
      • 2019-12-04
      • 2019-12-24
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 2021-10-02
      相关资源
      最近更新 更多