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

rbuckton picture rbuckton  ·  139Comments

blakeembrey picture blakeembrey  ·  171Comments

kimamula picture kimamula  ·  147Comments

born2net picture born2net  ·  150Comments

tenry92 picture tenry92  ·  146Comments