Typescript: 不能为此引用类型(在严格模式下)

创建于 2017-05-23  ·  1评论  ·  资料来源: microsoft/TypeScript



TypeScript版本: 2.2.1 /每晚(2.2.0-dev.201xxxxx)
2.3.3

严格是真的

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

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

a.test()

预期行为:
成功

实际行为:

Test.ts(2,17):错误TS2683:“ this”隐式具有类型“ any”,因为它没有类型注释。
Test.ts(3,18):错误TS2683:“ this”隐式具有类型“ any”,因为它没有类型注释。

Question

最有用的评论

在函数声明中指定this类型:

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

或切换--noImplicitThis false

>所有评论

在函数声明中指定this类型:

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

或切换--noImplicitThis false

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

Antony-Jones picture Antony-Jones  ·  3评论

DanielRosenwasser picture DanielRosenwasser  ·  3评论

dlaberge picture dlaberge  ·  3评论

uber5001 picture uber5001  ·  3评论

zhuravlikjb picture zhuravlikjb  ·  3评论