[英]class declaration - Property does not exist on type in TypeScript
I'm new to typescript
, and trying to for a javascript code I've into typescript, so I ended up with the below code.我是
typescript
新手,并尝试使用我已经进入 typescript 的 javascript 代码,所以我最终得到了下面的代码。 My understanding regarding the classes in typescript is the below, which may be wrong, is: 1. After defining the class, you have to declare the parameters to be used later on with this
, the deceleration is in the form of variableName:variableType
.我的关于打字稿类的理解是下面的,这可能是错误的,是:1。定义的类后,您必须声明的参数将与以后使用
this
,减速是形式variableName:variableType
。 2. in the constructor, the variables can assign values as this.variableName = x
3. In the methods
under the class
the variable can be used with this.
2. 在构造函数中,变量可以赋值为
this.variableName = x
3. 在class
下的methods
,变量可以与this.
一起使用this.
In the below code, I got the error [ts] Property 'escapeRegExp' does not exist on type 'typeof Listen'.
在下面的代码中,我收到错误
[ts] Property 'escapeRegExp' does not exist on type 'typeof Listen'.
as shown in the picture attached.如附图所示。
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
doesn't exist on a static method. this
在静态方法中不存在。 A static method is not tied to the class instance.静态方法不绑定到类实例。 There's a lot of reading on static methods, but basically: a class method can call a static method, a static method cannot call a class method (without an instance.)
有很多关于静态方法的阅读,但基本上:类方法可以调用静态方法,静态方法不能调用类方法(没有实例。)
The fix here is to remove static
from your methods.此处的解决方法是从您的方法中删除
static
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.