类型'BModel []'无法转换为'IBData'类型

时间:2017-01-19 10:35:09

标签: oop typescript

我有一个像这样的下面的类,它实现了一个IBData接口。

export class BModel implements IBData{

}

在API响应之后,我将数据分配给accountBalanceData类型的私有变量IBData

this.accountBalanceData  = data; 

我从api响应调用得到的数据是BModel类型,即类本身。

我收到以下error

[ts] Type 'BModel[]' is not assignable to type 'IBData'.
[ts]
Type 'BModel[]' is not assignable to type 'IBData'.
  Property 'getAcBal' is missing in type 'BModel[]'.

我在下面尝试了typecast

this.accountBalanceData  = data as IBData; 

我收到以下error

[ts] Type 'BModel[]' cannot be converted to type 'IBData'.
[ts]
Type 'BModel[]' cannot be converted to type 'IBData'.
  Property 'getAcBal' is missing in type 'BModel[]'.

更新

.map(res => {            
   res["_body"] = res["_body"].replace(/[\n\r\t]+/g,"");
   this.BModel.data = res["_body"]
   return this.BModel;
 }) 

1 个答案:

答案 0 :(得分:0)

函数.map(res => { res["_body"] = res["_body"].replace(/[\n\r\t]+/g,""); this.BModel.data = res["_body"] return this.BModel; }) 返回一个对象数组,因此该表达式的结果类型为

BModel[]

将是{{1}}