2017-06-02 156 views
0

我有一个非常简单的类,如:类型错误:不是一个函数打字稿

export class Party { 
    constructor(
     public id:Identifier, 
     public partyName: PartyName, 
     public person:Person 
    ) { } 

    copy():Party { 
     let copyParty = new Party(this.id, null, null); 
     return copyParty; 
    } 
} 

我想在另一个类(具体服务)使用拷贝功能(或方法?)导入这个类,如:

... (party => { 
let copyParty:Party = party.copy(); 
... 

,但我得到以下异常:

EXCEPTION: Uncaught (in promise): TypeError: party.copy is not a function 

我试过let copyParty:Party = Function.call(party.copy, copy)(有一些例外)以及let copyParty:Party = party.copy;(返回函数定义,而不是复制的对象)。

我在这里错过了什么? 谢谢。

+3

我的猜测是,'party'你,是不是实例化'Party'对象。它从何而来? – PierreDuc

+0

如果你输入你的参数'(party:Party =>)'它将帮助你追踪为什么一个party对象没有被传入。 –

+0

@PierreDuc'party'实例通过'.then(res => res.json()作为Party [])[0])的一个REST调用块 – suat

回答

5

您不能使用类型提示作为实际将对象转换为某种类型。它只是用来保持编译器的快乐,并且你的代码可读。

实际上,你应该在REST调用后实例化对象:

.then(res => res.json().map(party => new Party(party.id, party.partyName, party.person))[0] 

这样,你得到一个对象,它具有copy方法

相关问题