打字机中识别错误类型的打字稿

时间:2017-11-05 11:23:48

标签: angular typescript intellij-idea ionic-framework

我正在使用IntelliJ IDEA,在我的Ionic App中,我有以下功能

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

该功能正常,但我的IDE用红色标记了令牌并说:

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

1 个答案:

答案 0 :(得分:1)

您的代码:return new Promise(resolve => {

需要明确的注释

return new Promise<string>(resolve => {

为什么

因为无法推断它们的类型,因此会解析为{}