Kode Bryant
Kode Bryant

Reputation: 511

Typescript recognising wrong type in promise resolve

I'm using IntelliJ IDEA and in my Ionic App I have the following functions

private loadToken() {
  return new Promise(resolve => {
    this.storage.get('token').then(token=> {
      resolve(token && tokenNotExpired('token',token) ? token : null);
    });
  });
}
public authHeader() {
  return new Promise(resolve => {
    this.loadToken().then(token => {
      console.log(typeof token); //Returning 'string'
      let options = {
        headers: new HttpHeaders().set('Authorization', token) //token is underlined in red
      }
    });
  });
}

The function works fine, but my IDE underlines token in red and says:

Argument of type '{}' is not assignable to parameter of type 'string | string[]' ...

Upvotes: 0

Views: 160

Answers (1)

basarat
basarat

Reputation: 276363

Your code : return new Promise(resolve => {

Needs an explicit annotation

return new Promise<string>(resolve => {

Why

Because they type cannot be inferred and hence resolves to {}.

Upvotes: 1

Related Questions