FMR*_*FMR 1 typescript reactjs axios
我正在尝试使用 Axios 和 Typescript 在 React 应用程序中编写 API 服务。
这是我的代码:
interface Service<T> {
GetAll?: Promise<AxiosResponse<T>>;
}
interface Example {
id: Number;
}
const ApiService2 = () => {
const Example = (): Service<Example> => {
const GetAll = (): Promise<AxiosResponse<Example[]>> => {
return axios.get<Example[]>("urlhere");
};
return {
GetAll
};
};
};
Run Code Online (Sandbox Code Playgroud)
这是我的完整错误:
Type '() => Promise<AxiosResponse<Example[]>>' is missing the following properties from type 'Promise<AxiosResponse<Example>>': then, catch, [Symbol.toStringTag], finally TS2739
Run Code Online (Sandbox Code Playgroud)
问题是,你的定义interface Service有点错误。
的类型GetAll不应该是根据您的实现Promise<AxiosResponse<T>>返回的函数。Promise<AxiosResponse<T>>
所以你的Service界面将变成:
interface Service<T> {
GetAll?: () => Promise<AxiosResponse<T>>;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9456 次 |
| 最近记录: |