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?
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) { ... }
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With