簡體   English   中英

類聲明 - TypeScript 中的類型不存在屬性

[英]class declaration - Property does not exist on type in TypeScript

我是typescript新手,並嘗試使用我已經進入 typescript 的 javascript 代碼,所以我最終得到了下面的代碼。 我的關於打字稿類的理解是下面的,這可能是錯誤的,是:1。定義的類后,您必須聲明的參數將與以后使用this ,減速是形式variableName:variableType 2. 在構造函數中,變量可以賦值為this.variableName = x 3. 在class下的methods ,變量可以與this.一起使用this.

在下面的代碼中,我收到錯誤[ts] Property 'escapeRegExp' does not exist on type 'typeof Listen'. 如附圖所示。

namespace CORE{
    export class Listen{
         commandsList:[RegExp, string, string];
         debugStyle:string;
         optionalParam:RegExp;
         optionalRegex:RegExp;
         namedParam:RegExp;
         splatParam:RegExp;
         escapeRegExp:RegExp;
        constructor(){
            this.commandsList = [];
            this.debugStyle = 'font-weight: bold; color: #00f;';
            this.optionalParam = /\s*\((.*?)\)\s*/g;
            this.optionalRegex = /(\(\?:[^)]+\))\?/g;
            this.namedParam    = /(\(\?)?:\w+/g;
            this.splatParam    = /\*\w+/g;
            this.escapeRegExp  = /[\-{}\[\]+?.,\\\^$|#]/g;
        }

        public static commandToRegExp(command:string):RegExp{
            command = command.replace(this.escapeRegExp, '\\$&')
                    .replace(this.optionalParam, '(?:$1)?')
                    .replace(this.namedParam, function(match, optional) {
                        return optional ? match : '([^\\s]+)';
                    })
                    .replace(this.splatParam, '(.*?)')
                    .replace(this.optionalRegex, '\\s*$1?\\s*');

            return new RegExp('^' + command + '$', 'i');
        }

        public static registerCommand(command:RegExp, cb:string, phrase:string):void{
            this.commandsList.push({ command: command, callback: cb, originalPhrase: phrase });
        }

        public static addCommands(commands:string[]):void{
                  var cb;
                    for (var phrase in commands) {
                        if (commands.hasOwnProperty(phrase)) {
                            cb = this[commands[phrase]] || commands[phrase];
                            if (typeof cb === 'function') {
                                // convert command to regex then register the command
                                this.registerCommand(this.commandToRegExp(phrase), cb, phrase);
                            } else if (typeof cb === 'object' && cb.regexp instanceof RegExp) {
                                // register the command
                                this.registerCommand(new RegExp(cb.regexp.source, 'i'), cb.callback, phrase);
                            }
                        }
                    }
        }

        public static executeCommand(commandText:string):void{
            for (var j = 0, l = this.commandsList.length; j < l; j++) {
                var result = this.commandsList[j].command.exec(commandText);
                if (result) {
                    var parameters = result.slice(1);
                    // execute the matched command
                    this.commandsList[j].callback.apply(this, parameters);
                }
            }
        }        
    }
}

在此處輸入圖片說明

this在靜態方法中不存在。 靜態方法不綁定到類實例。 有很多關於靜態方法的閱讀,但基本上:類方法可以調用靜態方法,靜態方法不能調用類方法(沒有實例。)

此處的解決方法是從您的方法中刪除static

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM