简体   繁体   English

angular2 TS2322类型&#39;Promise <void> &#39;不可分配给&#39;Promise <string> &#39;当给定字符串时

[英]angular2 TS2322 Type 'Promise<void>' is not assignable to type 'Promise<string>' when given a string

Using Angular 2, I have a service that handles my API calls. 使用Angular 2,我有一个处理我的API调用的服务。 In the service, I have a handleError function that returns a Promise<string> for the error message. 在服务中,我有一个handleError函数,该函数为错误消息返回Promise<string> I'm trying to add some functionality to the handleError that will redirect the page afterwards to the login screen if there are credential errors. 我正在尝试向handleError添加一些功能,如果存在凭据错误,该功能将在以后将页面重定向到登录屏幕。 When I do that, TypeScript throws a TS2322 error even though I'm returning the same Promise and then trying to do some chains off of it. 当我这样做时,即使我返回相同的Promise,然后尝试从中删除链条,TypeScript也会引发TS2322错误。

error TS2322: Type 'Promise<void>' is not assignable to type 'Promise<string>'. 错误TS2322:类型'Promise <void>'无法分配给类型'Promise <string>'。 Type 'void' is not assignable to type 'string'. 类型“ void”不可分配给类型“ string”。

private handleError(error: Error): Promise<string> {
    let message = "Something went wrong loading data from the API.";
    if (error.response.status === 0) {
      message = "Unable to contact server.  Please file a bug report.";
    }
    if (error.message) {
      message = "API Service Error retrieving page: " + error.message;
    }
    this.alerts.error(message);
    if (error.response.status === 403) {
      if (error.message.indexOf("Authentication credentials") !== -1) {
          return Promise.reject<string>(message)  // this line errs with TS2322
          .then(() => this.appState.refresh())
          .then(() => this.redirects.login())
          .then(() => this.alerts.warning("Redirected to login because no credentials found."));
      } else {
          this.redirects.index();
      }
    }
    return Promise.reject<string>(message);
}
      return Promise.reject<string>(message)  // this line errs with TS2322
      .then(() => this.appState.refresh())
      .then(() => this.redirects.login())
      .then(() => this.alerts.warning("Redirected to login because no credentials found."));

Typescript is yelling at you because appState.refresh , redirects.login , or alerts.warning returns a void value. Typescript对您大吼大叫,因为appState.refreshredirects.loginalerts.warning返回空值。 To chain it correctly, set a variable with the chains, then return the variable: 为了正确地链接它,请使用链接设置一个变量,然后返回该变量:

    let result = Promise.reject<string>(message);

      result.then(() => this.appState.refresh())
            .then(() => this.redirects.login())
            .then(() => this.alerts.warning("Redirected to login because no credentials found."));

    return result;

It's worth mentioning also that none of the then s will be called because your Promise rejected. 值得一提的是,因为您的Promise被拒绝,所以不会调用then

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TS2322:类型&#39;(数据:TicketFullDTO)=&gt; 承诺<void> &#39; 不能分配给类型 &#39;FormEventHandler<HTMLFormElement> &#39; - TS2322: Type '(data: TicketFullDTO) => Promise<void>' is not assignable to type 'FormEventHandler<HTMLFormElement>' 角度错误 TS2322:&#39;Promise<Dish | undefined> &#39; 不可分配给类型 &#39;Promise<Dish> &#39; - Angular error TS2322:'Promise<Dish | undefined>' is not assignable to type 'Promise<Dish>' TS2322:输入&#39;Promise <Hero | undefined> &#39;不可分配给&#39;Promise <Hero> “ - TS2322: Type 'Promise<Hero | undefined>' is not assignable to type 'Promise<Hero>' 动态工厂方法-按字符串实例化类收到错误TS2322:类型“ Util”不能分配给类型“ void” - Dynamic Factory Method - Instantiate Class by String gets receives error TS2322: Type 'Util' is not assignable to type 'void' 角度:错误TS2322:类型&#39;Observable &lt;{}&gt;&#39;不可分配...使用share()运算符 - Angular: error TS2322: Type 'Observable<{}>' is not assignable … with share() operator TypeScript - TS2322:类型“string”不可分配给类型“str1”| “str2”&#39; - TypeScript - TS2322: Type 'string' is not assignable to type '“str1” | “str2”' React TypeScript 错误:TS2322 - 类型''不可分配给类型'IntrinsicAttributes &amp; String' - React TypeScript Error : TS2322 - Type '' is not assignable to type 'IntrinsicAttributes & String' 类型“元素”不可分配给类型“字符串”.ts(2322) - Type 'Element' is not assignable to type 'string'.ts(2322) 类型 'HTMLButtonElement' 不可分配给类型 'string'.ts(2322) - Type 'HTMLButtonElement' is not assignable to type 'string'.ts(2322) 类型“IntersectionObserver”不可分配给类型“null”。 TS2322 [React,intersectionObserver] - Type 'IntersectionObserver' is not assignable to type 'null'. TS2322 [React, intersectionObserver]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM