2016-06-15 3 views
1

완전히 이해할 수없는 뭔가를 놓쳐 야합니다. 오류를 이해하지 못합니다.오류 TS2322 : 'Comment []'유형에 'Comment []'을 할당 할 수 없습니다.

오류 메시지 :

wwwroot/app/comments/commentList.ts(25,13): error TS2322: Type 'Comment[]' is not assignable to type 'Comment[]'. 
Type 'Comment' is not assignable to type 'Comment'. 
wwwroot/app/comments/commentList.ts(25,13): error TS2322: Type 'Comment[]' is not assignable to type 'Comment[]'. 
Type 'Comment' is not assignable to type 'Comment'. 
Property 'ID' is missing in type 'Comment'. 

구성 요소 :

@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 *** 
     }); 
    } 

}

서비스 :

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; 
} 

모델 :

,691,363 (210)
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" 
    } 
} 
+0

질문을 편집하고'extractData'에 대한 코드를 추가 할 수 있습니까? –

+0

어떤 이상한 기회로 타이프 스크립트 팀원이 우연히이 글을 읽어야한다면 : 1.8.9 다음의 숫자는 1.8.10이 아닙니다 .... LOL 재미있는 내가 본 https://github.com/Microsoft/TypeScript/ 출시 – Sam

+0

마지막으로'10'을 확인한 후에 요청에 따라 '9' –

답변

0

이 그냥 추측입니다. 당신의 extractData 방법에서

당신은 일을하는지 : resangular Response object하고 문서 json() method 반환 any에 따라

return res.json() || [] 

.

그 이상인 경우 Comment은 인터페이스가 아닌 클래스이므로 json을 가져 와서 Comment으로 반환 할 수는 없습니다. 둘 사이의 속성이 일치하는 경우에도 마찬가지입니다.

당신은 수행해야합니다 : 당신이 그 문제를 가지고있는 것처럼

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; 
    } 
} 

다시 말하지만,이 문제가 있는지 확인하지,하지만 당신이 게시 코드에서 보인다.

+0

Nitzan, 통찰력을 주셔서 대단히 감사합니다. 내가 보여준 것과 같은 많은 서비스가 있고 그들은 모두 잘 작동하고 있습니다. 필자는 .map 파일이 내 프로젝트에서 다르게 나타나고있는 것으로 보아 이것이 Visual Studio의 문제 일 수 있다고 생각합니다. 그것은 파일이 .html -> .ts -> .js -> .map (순서가 잘못되었을 수도 있지만 계층 구조에 나타 났지만 지금은 그렇지 않음)와 같은 계층 구조에 나타났습니다. – Sam