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

angular, javascript, typescript, visual-studio-2017

Solution

You are not returning anything inside your `then` which makes `jobs` be of type `Promise<void>`. Return the array inside `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

Problem

I am new to angularJs2. I have created following service: ``` 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 When I compile the code using visual studio 2017 I get following error `**TS2322 Build:Type 'Promise<void>' is not assignable to type 'Promise<customType[]>'.*`* Help me to fix this error.

Original source