类型“AxiosResponse<any>”缺少类型“countries[]”中的以下属性:长度、弹出、推送、连接

Ben*_*ngy 3 typescript axios

在 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 }));
}
Run Code Online (Sandbox Code Playgroud)

vad*_*mk7 6

每个Axios 方法都使用泛型类型,因此您可以执行以下操作:

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