从调用中键入 Promise 泛型以获取?
Type Promise generic from call to fetch?
今天,我在尝试通过调用 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<{}> => {
知道如何在此处 键入响应而不必 在 apiCall
的 Response
泛型中提供 Response 对象的所有可能属性吗?谢谢 !
尝试使 apiCall 通用:
function apiCall<T>(url: string, requestOptions: RequestOptions = {}): Promise<T> {
...
}
调用此函数时,您可以将预期的类型传递给它:
const { someProp } = await apiCall<{someProp: type_of_some_prop}>(...);
今天,我在尝试通过调用 fetch
.
我有一个 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<{}> => {
知道如何在此处 键入响应而不必 在 apiCall
的 Response
泛型中提供 Response 对象的所有可能属性吗?谢谢 !
尝试使 apiCall 通用:
function apiCall<T>(url: string, requestOptions: RequestOptions = {}): Promise<T> {
...
}
调用此函数时,您可以将预期的类型传递给它:
const { someProp } = await apiCall<{someProp: type_of_some_prop}>(...);