Typescript 错误TS2322:类型';评论[]和#x27;不可分配给类型';评论[]和#x27;

Typescript 错误TS2322:类型';评论[]和#x27;不可分配给类型';评论[]和#x27;,typescript,Typescript,我一定是错过了一些非常明显的东西,我只是不理解这个错误 错误消息: 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

我一定是错过了一些非常明显的东西,我只是不理解这个错误

错误消息:

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

我不完全确定,这只是一个猜测

extractData
方法中,您正在执行以下操作:

return res.json() || []
其中
res
为,根据文档返回
any

更重要的是,您的
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;
    }
}
private-extractData(res:Response){
如果(分辨率状态<200 | |分辨率状态>=300){
抛出新错误(“错误响应状态:”+res.status);
}
return(res.json()| |[]).map(commentJson=>newcomment(commentJson));
}
接口注释JSON{
ID:编号;
//更多属性
}
导出类注释{
公众ID:号码;
//更多属性
构造函数(json:CommentJson){
this.ID=json.ID;
}
}

同样,我不确定这是否是您的问题,但从您发布的代码来看,您似乎有这些问题。

我不完全确定,这只是一个猜测

extractData
方法中,您正在执行以下操作:

return res.json() || []
其中
res
为,根据文档返回
any

更重要的是,您的
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;
    }
}
private-extractData(res:Response){
如果(分辨率状态<200 | |分辨率状态>=300){
抛出新错误(“错误响应状态:”+res.status);
}
return(res.json()| |[]).map(commentJson=>newcomment(commentJson));
}
接口注释JSON{
ID:编号;
//更多属性
}
导出类注释{
公众ID:号码;
//更多属性
构造函数(json:CommentJson){
this.ID=json.ID;
}
}

同样,我也不确定这是否是你的问题,但从你发布的代码来看,你似乎有这些问题。

你能编辑你的问题并添加
提取数据的代码吗?
?顺便说一句,如果任何打字稿团队成员偶然读到这篇文章:1.8.9后面的数字不是1.8.10。。。。哈哈,上次我检查
10
时看到的最有趣的事情发生在
9
根据您的请求添加的代码之后。我问了我的妻子,她是博士(不是开玩笑),她在大学里教统计学。。。她确认,当增加0.1时,1.8.9后面的数字是1.9.0(这很好,我们只是觉得很有趣),也许你在两个文件中引用了不同的
注释
类型?至于版本,
1.8.9
不是一个数字,它是一个发布名称,其中
1
主要的
8
次要的
9
补丁
。事实上,你增加补丁并不意味着你必须增加辅修。你能编辑你的问题并为
extractData
添加代码吗?顺便说一句,如果任何typescript团队成员偶然读到这篇文章:1.8.9后面的数字不是1.8.10。。。。哈哈,上次我检查
10
时看到的最有趣的事情发生在
9
根据您的请求添加的代码之后。我问了我的妻子,她是博士(不是开玩笑),她在大学里教统计学。。。她确认,当增加0.1时,1.8.9后面的数字是1.9.0(这很好,我们只是觉得很有趣),也许你在两个文件中引用了不同的
注释
类型?至于版本,
1.8.9
不是一个数字,它是一个发布名称,其中
1
主要的
8
次要的
9
补丁
。你增加补丁并不意味着你必须增加小调。尼赞,非常感谢你的洞察力。我有很多服务,就像上面展示的一样,它们都工作得很好。我怀疑这可能是visual studio的一个问题,因为我刚刚注意到.map文件在我的项目中的显示方式有所不同。过去,文件以.html->.ts->.js->.map的形式出现在层次结构中(我可能有错误的顺序,但它们出现在层次结构中,而现在没有)。Nitzan,非常感谢您的见解。我有很多服务,就像上面展示的一样,它们都工作得很好。我怀疑这可能是visual studio的一个问题,因为我刚刚注意到.map文件在我的项目中的显示方式有所不同。过去,文件以.html->.ts->.js->.map的形式出现在层次结构中(我可能有错误的顺序,但它们出现在层次结构中,而现在没有)。