typescript - Typ 'AxiosResponse' saknar följande egenskaper från typen 'countries[]': length, pop, push, concat
Vid setState får jag detta TS-fel:
"Typen 'AxiosResponse' saknar följande egenskaper från typen 'countries[]': length, pop, push, concat och 26 till.ts(2740)"
Kan någon föreslå vad som är fel/vart man ska använda :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 }));
}
Svar #1
Varje Axios-metod använder generiska typer så att du kan göra följande:
axios
.get<countries>('https://func-myportal-dev.azurewebsites.net/api/GetCountries?code=j0xGK8L6iRXpZqR3DgaDRoI/H/qjGZSGdBPIY9rZU84uH4SUa80noQ==')
.then(res => { /* res.data here is countries type */ })
