Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript error: Reserved word 'this' used as name

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:

Error example

Why can't the this keyword be a type? Am I perhaps using an old TypeScript compiler 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.

like image 533
Duncan Luk Avatar asked Mar 18 '16 20:03

Duncan Luk


1 Answers

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

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

like image 200
David Sherret Avatar answered Oct 29 '22 01:10

David Sherret