定义“此”变量的类型

时间:2018-08-14 12:18:04

标签: typescript this

我正在尝试使用Monaco编辑器来编辑函数主体。

我设法使用全局声明提供了参数及其类型,但我也想声明 this 上下文变量的类型。

我在网上找不到任何样品或信息,该怎么做。 有想法吗?

2 个答案:

答案 0 :(得分:0)

在您的示例中,您应该首先声明此变量...如

value1:string = "";

takeValue(arg1:string){

this.value1 = arg1; // here you add the value

}

答案 1 :(得分:0)

TS 2.0中已提供此功能。

请参阅Microsoft的官方公告:

https://github.com/Microsoft/TypeScript/pull/6739

例如如下所示:

class SomeType
{
    value1 = 'Some string'
}

function x(this:SomeType)
{
    console.log(this.value1)
    //your code...
}
相关问题