TypeError:不是TypeScript函数

sua*_*uat 1 javascript typescript angular

我有一个非常简单的类,如:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在另一个类(特别是服务)中使用copy函数(或方法?)来导入这个类,如:

... (party => {
let copyParty:Party = party.copy();
...
Run Code Online (Sandbox Code Playgroud)

但我得到以下异常:

EXCEPTION: Uncaught (in promise): TypeError: party.copy is not a function
Run Code Online (Sandbox Code Playgroud)

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

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

Pie*_*Duc 7

您不能使用类型提示作为实际将对象转换为特定类型.它只是用来让编译器开心,你的代码可读.

您应该在REST调用后实际实例化对象:

.then(res => res.json().map(party => new Party(party.id, party.partyName, party.person))[0]
Run Code Online (Sandbox Code Playgroud)

这样你就得到了一个拥有该copy方法的对象