Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'Promise<string[]>' is not assignable to type 'string[]'

Tags:

Getting following error: Type 'Promise<string[]>' is not assignable to type 'string[]'. Property 'includes' is missing in type 'Promise<string[]>'.

when i cast Promise<string[]> to type 'string[]' My code below,

Component: app.dashboard.ts

    import {Component} from '@angular/core';     import { MemberService } from "./app.service";     @Component({     selector:'app-root',     templateUrl:'./app.dashboard.html',     providers:[MemberService]               })      export class AppDashboard{       title='Dashboard'       constructor(private memberService: MemberService) { }        public doughnutChartLabels:string[] =          this.memberService.getmemberheader();//error occurred here       }     } 

Service:app.service.ts

    import { Injectable } from '@angular/core';     import { Member } from './Member';     import { Http, Response, Headers, RequestOptions, URLSearchParams } from'@angular/http';     import 'rxjs/add/operator/map';     import 'rxjs/add/operator/toPromise';      @Injectable()     export class MemberService     {       constructor(private http: Http) {       }            private getHeaders(){         // I included these headers because otherwise FireFox         // will request text/html instead of application/json         let headers = new Headers();         headers.append('Accept', 'application/json');         return headers;       }              getmemberheader(): Promise<string[]> {         return this.http             .get(`/ReportService/MemberDatabaseCountryname`, {headers: this.getHeaders()})             .toPromise()             .then(this.extractData)             .catch(this.handleError);       }          private extractData(res: Response) {         let body = res.json();                 return body || {};       }       private handleError(error: any): Promise<any> {         console.error('An error occurred', error);         return Promise.reject(error.message || error);       }     } 
like image 890
Arun kumar Avatar asked Aug 01 '17 06:08

Arun kumar


People also ask

How do I fix type string is not assignable to type never?

The error "Type is not assignable to type 'never'" occurs when we declare an empty array without explicitly typing it and attempt to mutate the array. To solve the error, explicitly type the empty array, e.g. const arr: string[] = []; .

Is not assignable to parameter of type?

The error "Argument of type string | undefined is not assignable to parameter of type string" occurs when a possibly undefined value is passed to a function that expects a string . To solve the error, use a type guard to verify the value is a string before passing it to the function.

How do I return a value from Promise TypeScript?

To access the value of a promise in TypeScript, call the then() method on the promise, e.g. p. then(value => console. log(value)) . The then() method takes a function, which is passed the resolved value as a parameter.


1 Answers

Assume your response from http.get is an array, here you are returning Promise from function memberService.getmemberheader, you should retrieve result of promise at its then callback instead (not assigning promise itself to array doughnutChartLabels).

public doughnutChartLabels: string[];  this.memberService.getmemberheader().then(res => {   this.doughnutChartLabels = res; }) 
like image 157
Pengyy Avatar answered Sep 21 '22 04:09

Pengyy