網站搜尋

程式碼翻譯 - TypeScript 編譯錯誤 無法調用缺少調用簽名的表達式


轉譯以下程式碼後的錯誤訊息:

[tsc] > C:/Workarea/MyFirstAngular/src/enum/msg.ts(35,33): 錯誤 TS2349: 無法調用其類型缺少調用簽名的表達式。TypeScript 編譯失敗:錯誤:tsc 命令已以代碼 2 結束

module MessageUtil {
    enum Morning {
    "Good Morning",
    "Great to see you!",
    "Good day.",
    "Lovely day today, isn't it?",
    "What's up?",
    "Nice to meet you",
}
}
    export class MessageData {
        private getRandomElementOfEnum(e : any):string{
            var length:number = Object.keys(e).length();  //<-- This is Line 35
            return e[Math.floor((Math.random() * length)+1)];
        }
        public getRandMorning():string {
            return this.getRandomElementOfEnum(Morning);
        }
    }
}

有人知道我究竟錯在哪裡嗎?

我的設定:- IDEA 14 - Node.js - Gulp - gulp-tsc - gulp-connect(用於實時重新載入)

答案#1

出現相同錯誤訊息的朋友 --> 檢查你的程式碼語法

找到我的錯誤。這不是Java。

 private getRandomElementOfEnum(e : any):string{
      var length:number = Object.keys(e).length();  //<-- This is Line 35
      return e[Math.floor((Math.random() * length)+1)];
 }

應該是:

    private getRandomElementOfEnum(e : any):string{
        var length:number = Object.keys(e).length;  // <--- WITHOUT ()
        return e[Math.floor((Math.random() * length)+1)];
    }