文章内容
2020/5/25 17:37:45,作 者: 黄兵
TS2322: Type 'string' is not assignable to type 'never'.
最近在处理Angular错误消息的时候出现如下错误:
TS2322: Type 'string' is not assignable to type 'never'.
出现错误的代码:
export class LinksComponent implements OnInit { sub: any; errorMessages: [] = []; constructor(private _linkService: LinksService) { } ngOnInit() { this._linkService.getLinks().subscribe((res: any) => { this.sub = res; }, (error: any) => { this.errorMessages.push({description: 'Server error. Try later.'}); }); } }
出现错误的原因:
errorMessages
未限定类型,而数组加入的是any类型,所以出现了以上错误。
解决方案:
限定errorMessages
数据类型。代码如下:
errorMessages: any[] = [];
参考资料:
1、What is “not assignable to parameter of type never” error in typescript?
黄兵个人博客原创。
转载请注明出处:黄兵个人博客 - TS2322: Type 'string' is not assignable to type 'never'.
评论列表