我看了一个使用 1.0.0 版本的 typescript 教程。 有一个类的示例,在构造函数中使用公共(public) rest 参数:
class XYZ {
constructor(public firstname: string, public lastname: string, ...public emails: Array<string>) {
}
}
在 1.5.0 版本中如何做到这一点? 如果我这样定义类,我会遇到几个错误:
type.ts(14,75): error TS1005: '=' expected.
type.ts(14,81): error TS1005: ',' expected.
type.ts(14,88): error TS1005: '=' expected.
type.ts(14,96): error TS1109: Expression expected.
谢谢 马里奥
最佳答案
There is an oversight in the spec ,但其余参数不能是公共(public)的或私有(private)的。以下是修复代码的方法:
class XYZ {
public emails: string[];
constructor(public firstName: string, public lastName: string, ...emails: string[]) {
this.emails = emails;
}
}
关于typescript - 构造函数中的公共(public)其余参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29697500/