Tom Kurian
Tom Kurian

Reputation: 113

Promises & TypeScript

I have a method that I must implement through an abstract class, which has a signature like this:

isAuthenticated(path: string): boolean

In the implementation I'm calling a promise from an authorization server

isAuthenticated(path: string): boolean {

this.authorization.isAuthenticated().then((res) => {
    if(res == true) {
      return true;
    }
    return false;
});
}

But the method gives me a error/warning like this:

A function whose type is neither declared type is neither 'void' nor 'any' must return a value

Upvotes: 1

Views: 274

Answers (1)

crashmstr
crashmstr

Reputation: 28563

You are not returning anything from isAuthenticated. You also cannot just "wait" here for the result that simply.

You can do this:

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;
  });
}

And allow the caller to "wait" for the boolean result.

Note: Assuming that this.authorization.isAuthenticated returns a Promise<boolean> and you don't need to do any other actions in the .then, the code could be simplified to:

isAuthenticated(path: string): Promise<boolean> {
  return this.authorization.isAuthenticated();
}

Upvotes: 1

Related Questions