在 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 }));
}
uj5u.com熱心網友回復:
每個Axios 方法都使用泛型型別,因此您可以執行以下操作:
axios
.get<countries>('https://func-myportal-dev.azurewebsites.net/api/GetCountries?code=j0xGK8L6iRXpZqR3DgaDRoI/H/qjGZSGdBPIY9rZU84uH4SUa80noQ==')
.then(res => { /* res.data here is countries type */ })
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/341112.html
上一篇:自定義未定義檢查的打字稿推斷
下一篇:使用打字稿泛型檢查物件是否為空
