Q:

How to solve "'this' implicitly has type 'any'" when Typescript checking classic JS class

I have some old code that uses classic JS classes that I want to type check. For example:
/**
 * @constructor
 */
function Test() {
    this.x = 1;
}

However when I run tsc --noImplicitThis --noEmit --allowJs --checkJs test.js to type check it, I get the following error:
test.js:5:5 - error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.

5     this.x = 1;
      ~~~~

I haven't been able to find any type annotation to fix this error, either by looking at https://github.com/Microsoft/TypeScript/wiki/JsDoc-support-in-JavaScript or just by guessing. Is there a way?

A:

It's noImplicitThis which produces that error. You need to use a this parameter to specify what the type of this is expected to be.
When using JSDoc you can use a @this annotation:
/**
 * @constructor
 * @this Test
 */
function Test() {
    this.x = 1;
}

In Typescript this is written as function Test(this: Point) { ... }.

