Typescript: Can not refer a type to this (in strict mode)

Created on 23 May 2017  ·  1Comment  ·  Source: microsoft/TypeScript



TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)
2.3.3

Code

strict is true

function test(){
    console.log(this.value);
    console.log((this as any).value);
}

let a = {
    value: 'a',
    test: test
}

a.test()

Expected behavior:
succ

Actual behavior:

Test.ts(2,17): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
Test.ts(3,18): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.

Question

Most helpful comment

Either specify a this type in your function declatation:

function test(this: any) {
    console.log(this.value);
}

or switch of --noImplicitThis false

>All comments

Either specify a this type in your function declatation:

function test(this: any) {
    console.log(this.value);
}

or switch of --noImplicitThis false

Was this page helpful?
0 / 5 - 0 ratings

Related issues

weswigham picture weswigham  ·  3Comments

kyasbal-1994 picture kyasbal-1994  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

Roam-Cooper picture Roam-Cooper  ·  3Comments

DanielRosenwasser picture DanielRosenwasser  ·  3Comments