2015-04-17 56 views
2

我觀看了使用版本1.0.0的打字稿教程。 有一類的樣本,使用在構造一個公共休息參數:構造函數中的公共靜態參數

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. 

感謝 馬里奧

回答

6

There is an oversight in the spec,但其餘的參數不能是公共或私有。這裏是如何修復代碼:

class XYZ { 
    public emails: string[]; 
    constructor(public firstName: string, public lastName: string, ...emails: string[]) { 
     this.emails = emails; 
    } 
} 
+1

它應該是'public emails:string []',以避免成員獲得'any'類型。 – Fenton

相關問題