我有一个必须通过抽象类实现的方法,它的签名如下:
isAuthenticated(path: string): boolean在实现中,我调用来自授权服务器的promise
isAuthenticated(path: string): boolean {
this.authorization.isAuthenticated().then((res) => {
if(res == true) {
return true;
}
return false;
});
}但是该方法给出了一个错误/警告,如下所示:
A function whose type is neither declared type is neither 'void' nor 'any' must return a value发布于 2018-11-20 23:01:50
您不会从isAuthenticated返回任何内容。你也不能在这里“等待”简单的结果。
您可以这样做:
isAuthenticated(path: string): Promise<boolean> {
// return the ".then" to return a promise of the type returned
// in the .then
return this.authorization.isAuthenticated().then((res) => {
if(res === true) {
return true;
}
return false;
});
}并允许调用者“等待”布尔结果。
注意:假设this.authorization.isAuthenticated返回一个Promise<boolean>,并且您不需要在.then中执行任何其他操作,则可以将代码简化为:
isAuthenticated(path: string): Promise<boolean> {
return this.authorization.isAuthenticated();
}https://stackoverflow.com/questions/53395729
复制相似问题