最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeScript error: Reserved word 'this' used as name - Stack Overflow

programmeradmin0浏览0评论

I am trying to get this Node.js TypeScript definition to work, but WebStorm gives me a big list of errors with all the same message:

Reserved word 'this' used as name. This inspection reports on any uses of JavaScript reserved words being used as a name. The JavaScript specification reserves a number of words which are currently not used as JavaScript keywords. Using those words as identifiers may result in broken code if later versions of JavaScript use them as keywords.

An example piece of code where this error happens due to the return type:

Why can't the this keyword be a type? Am I perhaps using an old TypeScript piler or is it a mistake in the typing?


Edit: To get rid of the errors I've just replaced all these this types with the type of the containing class or interface. For example, the errors in the given example are fixed by changing it to this:

export interface EventEmitter {
    addListener(event: string, listener: Function): EventEmitter;
    ...
}

Although, this is not a solution to the actual problem.

I am trying to get this Node.js TypeScript definition to work, but WebStorm gives me a big list of errors with all the same message:

Reserved word 'this' used as name. This inspection reports on any uses of JavaScript reserved words being used as a name. The JavaScript specification reserves a number of words which are currently not used as JavaScript keywords. Using those words as identifiers may result in broken code if later versions of JavaScript use them as keywords.

An example piece of code where this error happens due to the return type:

Why can't the this keyword be a type? Am I perhaps using an old TypeScript piler or is it a mistake in the typing?


Edit: To get rid of the errors I've just replaced all these this types with the type of the containing class or interface. For example, the errors in the given example are fixed by changing it to this:

export interface EventEmitter {
    addListener(event: string, listener: Function): EventEmitter;
    ...
}

Although, this is not a solution to the actual problem.

Share Improve this question edited Mar 18, 2016 at 21:43 Duncan Lukkenaer asked Mar 18, 2016 at 20:39 Duncan LukkenaerDuncan Lukkenaer 14.1k18 gold badges71 silver badges107 bronze badges 2
  • 3 This looks like a bug in WebStorm. – SLaks Commented Mar 18, 2016 at 20:45
  • I think SLaks is right. It works fine for me in Atom. – rgvassar Commented Mar 18, 2016 at 21:04
Add a ment  | 

3 Answers 3

Reset to default 3

Am I perhaps using an old TypeScript piler ... ?

Yes, upgrade to at least TS 1.7 to get polymorphic this support.

Seems you use old version of WebStorm. WebStorm supports 'this' type starting from v11.0.3.

The following code piles just fine in the typescript playground:

export interface E
{
    b(): this;
}

class A implements E
{
    public b(): any
    {
        return 123;
    }   
}

let a = new A();
console.log(a.b());

And according to link 'this' will be interpreted as 'any'. So most likely there is something with webstorm.

发布评论

评论列表(0)

  1. 暂无评论