typescript - 构造函数中的公共(public)其余参数

标签 typescript typescript1.4

我看了一个使用 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/

相关文章:

typescript - 为什么长度应该是索引器的子类型?

angularjs - Typescript + Angular "controller as"Sidewaffle 模板开箱即用

Angular 4 FormGroup removeControl() 不更新表单

angular - 模块解析失败 : *. ts 意外字符 '@'

node.js - 取决于多个并行 gulp 任务的完成

javascript - 如何检测 Firefox 中的 Enter 键?

javascript - 如何获取所有 Slide-Toggle 元素的 ID 和 Value

angularjs - IIS 7.5 中不允许使用 Type Script + Angular Js + Web API Status 405 的方法

javascript - JQuery DefinitelyTyped、typescript 1.4 和 Webstorm 错误突出显示

class - Typescript 手册中的继承示例