我有以下代码,当我尝试用“ResponseType”替换“any”时,出现错误
TS2345: Argument of type '(result: ResponseType) => void' is not assignable to parameter of type '(value: unknown) => void | PromiseLike<void>'. Types of parameters 'result' and 'value' are incompatible. Type 'unknown' is not assignable to type 'ResponseType'.
代码
interface ResponseType = {
foo: string,
bar: string
}
makeExternalRequest(param).
then((result : any) => {
const data = processData(result);
res.status(200).json(data });
}).catch((error) => {
const errorDetails = getErrorStatusAndMessage(error);
res.status(errorDetails.status).json({ message: errorDetails.message });
});
const makeExternalRequest = (param) => {
return got(
`https://somewebsite.com/${param}`,
).json();
};
为什么打字稿会抛出这个错误?应解决承诺,并且此类型应为非错误情况的响应类型。