[英]Typescript doesn't see return values from try/catch
I have a function that does some database operations.我有一个 function 可以执行一些数据库操作。 The implementation is wrapped in a
try/catch
block.该实现被包装在一个
try/catch
块中。 Eg例如
async function update({id, ...changes}): Promise<IUserResult> {
try {
//code implementation here
return updatedUser
} catch(error) {
console.error
}
}
Typescript compiler always throws an error that I return undefined. Typescript 编译器总是抛出我返回未定义的错误。 I know this is so because I don't return any value explicitly from the function block itself but from the
try/catch
block.我知道这是因为我没有从 function 块本身而是从
try/catch
块显式返回任何值。 How do I go about this?我该如何 go 关于这个? I am just learning typescript and I was wondering how to get the return values to match the function return type, in this case, the
IUserResult
.我刚刚学习 typescript ,我想知道如何让返回值与 function 返回类型相匹配,在这种情况下为
IUserResult
。
Thank you very much.非常感谢。
Typescript wants a valid return type for all cases, but you don't have any return in case of exception. Typescript 想要所有情况下的有效返回类型,但在出现异常时您没有任何返回。
You need to return something in your catch (or maybe finally) block or make return type like Promise<IUserResult|void>你需要在你的 catch (或者可能 finally )块中返回一些东西,或者让返回类型像 Promise<IUserResult|void>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.