typescript 类型 Promise<void> 不可分配给类型 Promise<customType[]>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43860791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:31:56  来源:igfitidea点击:

Type Promise<void> is not assignable to type Promise<customType[]>

javascriptangulartypescriptvisual-studio-2017

提问by MARKAND Bhatt

I am new to angularJs2. I have created following service:

我是 angularJs2 的新手。我创建了以下服务:

import { Injectable, OnInit } from '@angular/core';
import { customType } from '../models/currentJobs';
import { Headers, Http } from '@angular/http';

import 'rxjs/add/operator/toPromise';

@Injectable()
export class JobService implements OnInit {

    constructor(private http: Http) { }

    ngOnInit(): void {
        this.getCurrentJobs();
    }

    private headers: Headers = new Headers({ 'Content-Type': 'application/json' });
    private ordersUrl: string = 'http://localhost:35032/api/order/';

    public orders: customType[];

    getCurrentJobs(): Promise<customType[]> {
        var jobs =  this.http.get(this.ordersUrl)
            .toPromise()
            .then(response => {
                this.orders = response.json() as customType[];
            })
            .catch(this.handleError);
        return jobs;//this line throws error
    }

    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error);
        return Promise.reject(error.message || error);
    }
}

Following are my Typescript compile configuration of Vs2017

以下是我对 Vs2017 的 Typescript 编译配置

enter image description here

在此处输入图片说明

When I compile the code using visual studio 2017 I get following error

当我使用 Visual Studio 2017 编译代码时,出现以下错误

**TS2322 Build:Type 'Promise<void>' is not assignable to type 'Promise<customType[]>'.**

**TS2322 Build:Type 'Promise<void>' is not assignable to type 'Promise<customType[]>'.**

Help me to fix this error.

帮我修复这个错误。

回答by Saravana

You are not returning anything inside your thenwhich makes jobsbe of type Promise<void>. Return the array inside then:

你是不是给您回里的任何东西then,这使得jobs具有类型Promise<void>。返回里面的数组then

getCurrentJobs(): Promise<customType[]> {
    var jobs = this.http.get(this.ordersUrl)
      .toPromise()
      .then(response => {
        this.orders = response.json() as customType[];
        return this.orders;
      })
      .catch(this.handleError);
    return jobs;
}

See the chaining behaviour of promises: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Chaining

查看 promise 的链接行为:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Chaining

回答by Stephen R. Smith

I've added the 'catch' operator, and swapped your interface import for an interface definition in the code (as I don't obviously have access to yours). I can't really test this without the rest of your project code, but it looks right to me and doesn't throw any errors in VSC.

我添加了“catch”运算符,并将您的接口导入替换为代码中的接口定义(因为我显然无法访问您的)。如果没有您的项目代码的其余部分,我无法真正测试它,但它在我看来是正确的,并且不会在 VSC 中引发任何错误。

import { Injectable, OnInit } from '@angular/core';
import { Headers, Http } from '@angular/http';

import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/catch';


export interface customType{
}


@Injectable()
export class JobService implements OnInit {

    constructor(private http: Http) { }
    private jobs: Promise<customType[]>;

    ngOnInit(): void {
        this.jobs = this.getCurrentJobs();
    }

    private headers: Headers = new Headers({ 'Content-Type': 'application/json' });
    private ordersUrl: string = 'http://localhost:35032/api/order/';

    public orders: customType[];

    getCurrentJobs(): Promise<customType[]> {
        return this.http.get(this.ordersUrl)
          .map(response => response.json())
          .catch(this.handleError)
          .toPromise();

    }

    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error);
        return Promise.reject(error.message || error);
    }
}