今天,我在尝试通过调用 fetch
键入 Promise 时遇到了一个小问题。 .
我有一个callAPI
助手的签名是:
const apiCall = (url: string, requestOptions: RequestOptions = {}):
Promise<{}>
现在,我在其他助手中使用它,其签名如下所示:
const getProps = async (
// args
): Promise<{}> => {
const { someProp } = await apiCall(
// build query with args here
);
return someProp;
};
不幸的是,flow
对此有所提示,但我还没有设法在此处输入回复。
Cannot call await with apiCall(...) bound to p because property someProp is missing in object type [1] in type argument
R [2].
/private/tmp/flow/flowlib_65ddbae/core.js
[2] 583│ declare class Promise<+R> {
apiCall.js
[1] 43│ const apiCall = (url: string, requestOptions: RequestOptions = {}): Promise<{}> => {
知道如何在此处键入响应而无需在 Response
中提供 Response 对象的所有可能属性。 apiCall
的通用?谢谢 !
最佳答案
尝试使 apiCall 通用:
function apiCall<T>(url: string, requestOptions: RequestOptions = {}): Promise<T> {
...
}
当您调用此函数时,您可以将预期的类型传递给它:
const { someProp } = await apiCall<{someProp: type_of_some_prop}>(...);
关于javascript - 从调用到获取的类型 Promise 是通用的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52401613/