typescript - error TS2322: Type 'Comment[]' is not assignable to type 'Comment[]' -


i must missing obvious not understand error.

error message:

wwwroot/app/comments/commentlist.ts(25,13): error ts2322: type 'comment[]' not assignable type 'comment[]'. type 'comment' not assignable type 'comment'. wwwroot/app/comments/commentlist.ts(25,13): error ts2322: type 'comment[]' not assignable type 'comment[]'. type 'comment' not assignable type 'comment'. property 'id' missing in type 'comment'. 

component:

@component({    selector: 'blog-comment-list',    templateurl: './app/comments/commentlist.html' }) export class commentlist implements oninit {     @input() contentitemid: number;     private comments: comment[];      constructor(private sessionservice: sessionservice, private blogservice: blogservice) {      }      ngoninit() {           this.blogservice.getcommentsforcontentitem(this.contentitemid).subscribe(x => {             this.comments = x;  // *** build error here ***         });     } 

}

service:

getcommentsforcontentitem(contentitemid: number): observable<comment[]> {     let url = this.serviceurl + 'getcommentsforcontentitem?contentitemid=' + contentitemid.tostring();     return this.http.get(this.nocache(url))         .map(response => this.extractdata(response))         .catch(this.handleerror); }  private extractdata(res: response) {     if (res.status < 200 || res.status >= 300) {         throw new error('bad response status: ' + res.status);     }     let body = res.json();     return body || []; }  private handleerror(error: any) {     let errormsg = error.message || 'server errorx';     console.error(errormsg);     return observable.throw(errormsg); }  private nocache(url: string): string {     if (url === null || typeof (url) === 'undefined')         return null;      if (url.slice(-1) === '/')         url = url.slice(0, -1);      let connector = url.includes('?') ? '&' : '?';      url = url + connector + 'nocache=' + (math.random().tostring().replace('.', ''));     return url; } 

model:

export class comment {     public id: number;     // more properties } 

package.json

{   "version": "1.0.0",   "name": "samsblog",   "private": true,   "dependencies": {     "@angular/common": "2.0.0-rc.1",     "@angular/compiler": "2.0.0-rc.1",     "@angular/core": "2.0.0-rc.1",     "@angular/http": "2.0.0-rc.1",     "@angular/platform-browser": "2.0.0-rc.1",     "@angular/platform-browser-dynamic": "2.0.0-rc.1",     "@angular/router": "2.0.0-rc.1",     "@angular/router-deprecated": "2.0.0-rc.1",     "@angular/upgrade": "2.0.0-rc.1",     "bootstrap": "^3.3.6",     "es6-shim": "^0.35.0",     "reflect-metadata": "^0.1.3",     "rxjs": "5.0.0-beta.6",     "systemjs": "0.19.27",     "core-js": "^2.4.0",     "zone.js": "^0.6.12"   },   "devdependencies": {     "del": "2.2.0",     "gulp": "^3.9.1",     "gulp-clean": "^0.3.2",     "gulp-concat": "^2.6.0",     "gulp-ignore": "^2.0.1",     "gulp-minify": "0.0.12",     "gulp-rename": "^1.2.2",     "gulp-typescript": "^2.13.4",     "gulp-uglify": "^1.5.3",     "gulp-util": "^3.0.7",     "typescript": "^1.8.10",     "typings": "^1.0.4"   } } 

i'm not sure it, guess.

in extractdata method you're doing:

return res.json() || [] 

where res angular response object , according docs json() method returns any.

more that, comment class not interface, can't take json , return comment if there's complete match properties between them.

you'll need do:

private extractdata(res: response) {     if (res.status < 200 || res.status >= 300) {         throw new error('bad response status: ' + res.status);     }      return (res.json() || []).map(commentjson => new comment(commentjson)); }  interface commentjson {     id: number;     // more properties }  export class comment {     public id: number;     // more properties      constructor(json: commentjson) {         this.id = json.id;     } } 

again, not sure problem, code posted seems have issues.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -