我正在使用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[]' ...
答案 0 :(得分:1)
您的代码:return new Promise(resolve => {
需要明确的注释
return new Promise<string>(resolve => {
因为无法推断它们的类型,因此会解析为{}
。