Typescript: noImplicitAny does not apply to function context

Created on 13 Feb 2019  ·  2Comments  ·  Source: microsoft/TypeScript


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

Working as Intended

Most helpful comment

noImplicitThis controls this behavior rather than noImplicitAny. You can turn it on in the Playground to try.

All 2 comments

noImplicitThis controls this behavior rather than noImplicitAny. You can turn it on in the Playground to try.

Good to know. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Roam-Cooper picture Roam-Cooper  ·  3Comments

wmaurer picture wmaurer  ·  3Comments

kyasbal-1994 picture kyasbal-1994  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

weswigham picture weswigham  ·  3Comments