TypeScript Version: 3.3.3
Search Terms: noImplicitAny, context
Code
function foo(num: number) {
this.num = num;
}
Expected behavior:
When I hover over this I should see void as the implicitly inferred type. Ideally this.num would be an error, especially in strict mode.
Actual behavior:
I see any as the implicit type of this. this is always inferred to be any unless I override it in the function signature.
Playground Link:
Bad: https://www.typescriptlang.org/play/index.html#src=type%20Context%20%3D%20%7B%0D%0A%20%20%20%20num%3A%20number%2C%0D%0A%7D%0D%0A%0D%0Afunction%20foo(num%3A%20number)%20%7B%0D%0A%20%20this.num%20%3D%20num%3B%0D%0A%7D
Good: https://www.typescriptlang.org/play/index.html#src=type%20Context%20%3D%20%7B%0D%0A%20%20%20%20num%3A%20number%2C%0D%0A%7D%0D%0A%0D%0Afunction%20foo(this%3A%20Context%2C%20num%3A%20number)%20%7B%0D%0A%20%20this.num%20%3D%20num%3B%0D%0A%7D
noImplicitThis controls this behavior rather than noImplicitAny. You can turn it on in the Playground to try.
Good to know. Thanks!
Most helpful comment
noImplicitThiscontrols this behavior rather thannoImplicitAny. You can turn it on in the Playground to try.